OperationInvoker.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:OperationInvoker.cs
  5. * 2.功能描述:服务操作者
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ServiceModel;
  12. using Dongke.IBOSS.PRD.Basics.BaseResources;
  13. using Dongke.IBOSS.PRD.Basics.Library;
  14. namespace Dongke.IBOSS.PRD.WCF.Proxys.ServiceProxy
  15. {
  16. /// <summary>
  17. /// 服务操作者
  18. /// </summary>
  19. public class OperationInvoker
  20. {
  21. /// <summary>
  22. /// 执行
  23. /// </summary>
  24. /// <typeparam name="TChannel"></typeparam>
  25. /// <param name="serviceInvocation"></param>
  26. /// <param name="channel"></param>
  27. public static void Invoke<TChannel>(Action<TChannel> serviceInvocation, TChannel channel)
  28. {
  29. ICommunicationObject communicationObject = (ICommunicationObject)channel;
  30. try
  31. {
  32. serviceInvocation(channel);
  33. communicationObject.Close();
  34. }
  35. catch (Exception ex)
  36. {
  37. HandleException(ex, communicationObject);
  38. }
  39. }
  40. /// <summary>
  41. /// 执行
  42. /// </summary>
  43. /// <typeparam name="TChannel"></typeparam>
  44. /// <typeparam name="TResult"></typeparam>
  45. /// <param name="serviceInvocation"></param>
  46. /// <param name="channel"></param>
  47. /// <returns></returns>
  48. public static TResult Invoke<TChannel, TResult>(Func<TChannel, TResult> serviceInvocation, TChannel channel)
  49. {
  50. ICommunicationObject communicationObject = (ICommunicationObject)channel;
  51. TResult result = default(TResult);
  52. try
  53. {
  54. result = serviceInvocation(channel);
  55. communicationObject.Close();
  56. }
  57. catch (Exception ex)
  58. {
  59. HandleException(ex, communicationObject);
  60. }
  61. return result;
  62. }
  63. /// <summary>
  64. /// 异常处理
  65. /// </summary>
  66. /// <param name="ex"></param>
  67. /// <param name="channel"></param>
  68. public static void HandleException(Exception ex, ICommunicationObject channel)
  69. {
  70. channel.Abort();
  71. /*
  72. // 通信错误 诸如网络错误,地址错误,服务器没有启动等等
  73. // 状态异常 代理已经关闭,或者通道Fault,等问题
  74. // 服务异常 服务调用时抛出的异常,这个服务内部异常会序列化传递给客户端,被客户端捕获
  75. if (ex is CommunicationException)
  76. {
  77. // 通信错误,诸如网络错误,地址错误,服务器没有启动,验证没有通过等等
  78. }
  79. else if (ex is TimeoutException)
  80. {
  81. // 通信错误,超时
  82. }
  83. //else if (ex is FaultException<ExceptionDetail>)
  84. //{
  85. // // includeExceptionDetailInFaults="true"
  86. // //FaultException<ExceptionDetail> ded = (ex as FaultException<ExceptionDetail>);
  87. // // 服务端 应用异常
  88. //}
  89. else if (ex is FaultException)
  90. {
  91. // 服务端 应用异常
  92. }
  93. else if (ex is Exception)
  94. {
  95. }
  96. */
  97. throw ex;
  98. }
  99. }
  100. }