ServiceInvoker.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ServiceHostCollection.cs
  5. * 2.功能描述:服务集合
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/16 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using Dongke.IBOSS.PRD.Basics.DataAccess;
  12. using Dongke.IBOSS.PRD.Basics.Library;
  13. using Dongke.IBOSS.PRD.Service.PDAModuleLogic;
  14. using Dongke.IBOSS.PRD.WCF.DataModels;
  15. namespace Dongke.IBOSS.PRD.WCF.Services
  16. {
  17. public class ServiceInvoker
  18. {
  19. #region 服务事件
  20. /// <summary>
  21. /// 服务开始
  22. /// </summary>
  23. public static event ServiceDoingEventHandler ServiceDoingEvent;
  24. /// <summary>
  25. /// 服务结束
  26. /// </summary>
  27. public static event ServiceDoneEventHandler ServiceDoneEvent;
  28. /// <summary>
  29. /// 服务异常
  30. /// </summary>
  31. public static event ServiceExceptionEventHandler ServiceExceptionEvent;
  32. #endregion 服务事件
  33. /// <summary>
  34. /// 执行
  35. /// </summary>
  36. /// <param name="sender"></param>
  37. /// <param name="serviceInvocation"></param>
  38. public static void Invoke(object sender, Action serviceInvocation)
  39. {
  40. ServiceEventArgs eventArgs = new ServiceEventArgs();
  41. eventArgs.Sender = sender;
  42. eventArgs.ServiceName = sender.ToString();
  43. eventArgs.MethodName = serviceInvocation.Method.Name;
  44. BeginInvoke(eventArgs);
  45. try
  46. {
  47. serviceInvocation();
  48. }
  49. catch (Exception ex)
  50. {
  51. eventArgs.Exception = ex;
  52. InvokeException(eventArgs);
  53. HandleException(eventArgs);
  54. }
  55. finally
  56. {
  57. EndInvoke(eventArgs);
  58. }
  59. }
  60. /// <summary>
  61. /// 执行
  62. /// </summary>
  63. /// <typeparam name="TResult"></typeparam>
  64. /// <param name="sender"></param>
  65. /// <param name="serviceInvocation"></param>
  66. /// <param name="webUserInfo"></param>
  67. /// <returns></returns>
  68. public static TResult Invoke<TResult>(object sender, Func<TResult> serviceInvocation)
  69. {
  70. ServiceEventArgs eventArgs = new ServiceEventArgs();
  71. eventArgs.Sender = sender;
  72. eventArgs.ServiceName = sender.ToString();
  73. eventArgs.MethodName = serviceInvocation.Method.Name;
  74. BeginInvoke(eventArgs);
  75. TResult result = default(TResult);
  76. try
  77. {
  78. result = serviceInvocation();
  79. }
  80. catch (Exception ex)
  81. {
  82. eventArgs.Exception = ex;
  83. InvokeException(eventArgs);
  84. HandleException(eventArgs);
  85. }
  86. finally
  87. {
  88. EndInvoke(eventArgs);
  89. }
  90. return result;
  91. }
  92. /// <summary>
  93. /// 执行
  94. /// </summary>
  95. /// <typeparam name="TResult"></typeparam>
  96. /// <param name="sender"></param>
  97. /// <param name="serviceInvocation"></param>
  98. /// <param name="webUserInfo"></param>
  99. /// <returns></returns>
  100. public static ServiceResultEntity Invoke(object sender, Func<ServiceResultEntity> serviceInvocation)
  101. {
  102. ServiceEventArgs eventArgs = new ServiceEventArgs();
  103. eventArgs.Sender = sender;
  104. eventArgs.ServiceName = sender.ToString();
  105. eventArgs.MethodName = serviceInvocation.Method.Name;
  106. BeginInvoke(eventArgs);
  107. ServiceResultEntity result = null;
  108. try
  109. {
  110. result = serviceInvocation();
  111. }
  112. catch (Exception ex)
  113. {
  114. eventArgs.Exception = ex;
  115. InvokeException(eventArgs);
  116. HandleException(eventArgs);
  117. //OutputLog.TraceLog(LogPriority.Error,
  118. // eventArgs.Sender.ToString(),
  119. // eventArgs.MethodName,
  120. // eventArgs.Exception.ToString(),
  121. // LocalPath.LogExePath);
  122. //result = new ServiceResultEntity();
  123. //result.Status = Basics.BaseResources.Constant.ServiceResultStatus.SystemError;
  124. //result.Message = ex.ToString();
  125. ////result.Exception = ex;
  126. }
  127. finally
  128. {
  129. EndInvoke(eventArgs);
  130. }
  131. return result;
  132. }
  133. /// <summary>
  134. /// 开始执行
  135. /// </summary>
  136. /// <param name="eventArgs"></param>
  137. public static void BeginInvoke(ServiceEventArgs eventArgs)
  138. {
  139. if (ServiceDoingEvent != null)
  140. {
  141. ServiceDoingEvent(eventArgs.Sender, eventArgs);
  142. }
  143. }
  144. /// <summary>
  145. /// 执行异常
  146. /// </summary>
  147. /// <param name="eventArgs"></param>
  148. public static void InvokeException(ServiceEventArgs eventArgs)
  149. {
  150. if (ServiceExceptionEvent != null)
  151. {
  152. ServiceExceptionEvent.BeginInvoke(eventArgs.Sender, eventArgs, null, null);
  153. }
  154. }
  155. /// <summary>
  156. /// 结束执行
  157. /// </summary>
  158. /// <param name="eventArgs"></param>
  159. public static void EndInvoke(ServiceEventArgs eventArgs)
  160. {
  161. if (ServiceDoneEvent != null)
  162. {
  163. ServiceDoneEvent.BeginInvoke(eventArgs.Sender, eventArgs, null, null);
  164. }
  165. }
  166. /// <summary>
  167. /// 异常处理
  168. /// </summary>
  169. /// <param name="ex"></param>
  170. public static void HandleException(ServiceEventArgs ex)
  171. {
  172. string em = ex.Exception.Message;
  173. if (em.Contains("LoginInfoError") ||
  174. em.Contains("RepeatLogin") ||
  175. em.Contains("LicInfoError")
  176. )
  177. {
  178. throw new Exception(ex.MethodName, ex.Exception);
  179. }
  180. //LogFileOperation.Error(DataManager.LogFileName, ex.MethodName + " " + ex.Exception.Message);
  181. string userInfo = null;
  182. if (ex.Sender is ServicesBase)
  183. {
  184. ServicesBase sb = ex.Sender as ServicesBase;
  185. if (sb != null)
  186. {
  187. if (sb.SUserInfo != null)
  188. {
  189. userInfo = JsonHelper.ToJson(sb.SUserInfo) + System.Environment.NewLine;
  190. }
  191. }
  192. }
  193. OutputLog.TraceLog(LogPriority.Error,
  194. ex.Sender.ToString(),
  195. userInfo + ex.MethodName,
  196. ex.Exception.ToString(),
  197. LocalPath.LogExePath);
  198. // TODO 异常处理
  199. throw new Exception(ex.MethodName, ex.Exception);
  200. }
  201. }
  202. }