Program.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.Reflection;
  10. using Autofac;
  11. using EMIS.CommonLogic;
  12. using System.Configuration;
  13. using EMIS.Utility;
  14. using Bowin.Common.Log;
  15. using EMIS.CommonLogic.SystemServices;
  16. using Senparc.Weixin.TenPay.V3;
  17. using EMIS.Web.Controls;
  18. namespace EMIS.Services
  19. {
  20. public class Program : ServiceBase
  21. {
  22. private System.ComponentModel.Container components = null;
  23. private System.Timers.Timer m_timer;
  24. private int m_errornum = 0;
  25. //private Log _log = new Log();
  26. private string sServiceName = "";
  27. private bool isRunning = false;
  28. public static readonly Autofac.IContainer AutofacContainer;
  29. static Program()
  30. {
  31. var builder = new ContainerBuilder();
  32. builder.RegisterModule<EMIS.CommonLogic.DALModule>();
  33. builder.RegisterModule<EMIS.CommonLogic.ServiceModule>();
  34. var _container = builder.Build();
  35. AutofacContainer = _container;
  36. AutofacHelper.Container = _container;
  37. }
  38. public Program()
  39. {
  40. // 该调用是 Windows.Forms 组件设计器所必需的。
  41. InitializeComponent();
  42. }
  43. #region 进程的主入口点
  44. static void Main()
  45. {
  46. System.ServiceProcess.ServiceBase[] ServicesToRun;
  47. //System.Diagnostics.Debugger.Launch();
  48. // 同一进程中可以运行多个用户服务。若要将
  49. // 另一个服务添加到此进程,请更改下一行
  50. // 以创建另一个服务对象。例如,
  51. //
  52. // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
  53. //
  54. ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Program() };
  55. System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  56. }
  57. #endregion
  58. #region InitializeComponent
  59. /// <summary>
  60. /// 设计器支持所需的方法 - 不要使用代码编辑器
  61. /// 修改此方法的内容。
  62. /// </summary>
  63. private void InitializeComponent()
  64. {
  65. components = new System.ComponentModel.Container();
  66. helpInitializeComponent();
  67. }
  68. private void helpInitializeComponent()
  69. { //防止对设计视图的不可观察影响. GaoXT20090206
  70. this.CanHandlePowerEvent = true;
  71. this.CanPauseAndContinue = true;
  72. this.CanShutdown = true;
  73. sServiceName = ConfigurationManager.AppSettings["ServiceName"];
  74. this.ServiceName = "BackEnd Service For " + sServiceName;
  75. }
  76. #endregion
  77. #region Dispose
  78. /// <summary>
  79. /// 清理所有正在使用的资源。
  80. /// </summary>
  81. protected override void Dispose(bool disposing)
  82. {
  83. if (disposing)
  84. {
  85. if (components != null)
  86. {
  87. components.Dispose();
  88. }
  89. }
  90. base.Dispose(disposing);
  91. }
  92. #endregion
  93. #region OnStart
  94. /// <summary>
  95. /// 设置具体的操作,以便服务可以执行它的工作。
  96. /// </summary>
  97. protected override void OnStart(string[] args)
  98. {
  99. LogHelper.WriteLog(LogType.ServiceLog, "服务开始!");
  100. //获取时间设置的缺省值
  101. string timeInterVal = ConfigurationManager.AppSettings["Interval"];
  102. //将缺省值设置间隔时间设为计时器的间隔时间
  103. this.m_timer = new System.Timers.Timer();
  104. if (timeInterVal.Length != 0)
  105. {
  106. this.m_timer.Interval = System.Convert.ToInt32(timeInterVal);
  107. }
  108. else
  109. {
  110. this.m_timer.Interval = 10000; //缺省为10秒
  111. }
  112. this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimedEvent);
  113. this.m_timer.AutoReset = true;
  114. this.m_timer.Enabled = true;
  115. this.m_timer.Start();
  116. }
  117. #endregion
  118. #region 时间器运行方法,调用ServiceWrapper
  119. /// <summary>
  120. /// 时间器运行方法,调用ServiceWrapper
  121. /// </summary>
  122. /// <param name="source"></param>
  123. /// <param name="e"></param>
  124. protected void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
  125. {
  126. if (this.isRunning)
  127. {
  128. return;
  129. }
  130. this.isRunning = true;
  131. try
  132. {
  133. if (this.m_errornum < 100000)
  134. {
  135. try
  136. {
  137. LogHelper.WriteLog(LogType.ServiceLog, "----开始根据微信订单同步缴费状态-----");
  138. using (var scop = Program.AutofacContainer.BeginLifetimeScope())
  139. {
  140. IWechatPayServices WechatPayServices = scop.Resolve<IWechatPayServices>();
  141. var wecharPayList = WechatPayServices.SearchWeChatPayList();
  142. List<OrderQueryResult> orderList = new List<OrderQueryResult>();
  143. foreach (var wecharPay in wecharPayList)
  144. {
  145. OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID);
  146. orderList.Add(query);
  147. }
  148. WechatPayServices.UpdateWeChatPay(orderList);
  149. }
  150. LogHelper.WriteLog(LogType.ServiceLog, "----结束根据微信订单同步缴费状态-----");
  151. }
  152. catch (System.Exception err)
  153. {
  154. LogHelper.WriteLog(LogType.ServiceLog, "----根据微信订单同步缴费状态处理异常-----");
  155. LogHelper.WriteLog(LogType.ServiceLog, err.Message);
  156. //result = false;
  157. }
  158. ////动态创建Wrapper,并将基本值赋予其属性
  159. //string sWrapper = ConfigurationManager.AppSettings["Wrapper"];
  160. //string[] sArray = sWrapper.Split(',');
  161. //Assembly assem = Assembly.Load(sArray[0].ToString());
  162. //Type classType = assem.GetType(sArray[1].ToString());
  163. ////EMIS.Services.MyEAPServiceWrapper.IServiceWrapper fss = (EMIS.Services.MyEAPServiceWrapper.IServiceWrapper)System.Activator.CreateInstance(classType);
  164. //fss.IsDebug = System.Convert.ToBoolean(ConfigurationManager.AppSettings["IsDebug"]);
  165. //bool returnValue = true;
  166. //returnValue = fss.Execute(); //运行Wrapper
  167. //if (!returnValue)
  168. // this.m_errornum++;
  169. }
  170. else
  171. {
  172. LogHelper.WriteErrorLog(LogType.ServiceLog, "more than 100000 exceptions occurred when run ServiceWrapper, begin to stop service");
  173. this.OnStop();
  174. }
  175. }
  176. catch (Exception ce)
  177. {
  178. LogHelper.WriteErrorLog(LogType.ServiceLog, "Exception occur when run EAPService.OnTimedEvent,pls refer :" + ce.Message + "||" + ce.TargetSite + "||" + ce.StackTrace);
  179. this.m_errornum++;
  180. }
  181. finally
  182. {
  183. this.isRunning = false;
  184. }
  185. }
  186. #endregion
  187. #region OnStop
  188. /// <summary>
  189. /// 停止此服务。
  190. /// </summary>
  191. protected override void OnStop()
  192. {
  193. try
  194. {
  195. this.m_timer.Enabled = false;
  196. this.m_timer.Stop();
  197. this.m_timer.Close();
  198. this.m_errornum = 0;
  199. LogHelper.WriteLog(LogType.ServiceLog, "机器服务【" + sServiceName + "】结束!");
  200. }
  201. catch (Exception ce)
  202. {
  203. LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message);
  204. }
  205. }
  206. #endregion
  207. #region OnPause
  208. /// <summary>
  209. /// 重载暂停运行方法
  210. /// </summary>
  211. protected override void OnPause()
  212. {
  213. try
  214. {
  215. this.m_timer.AutoReset = false;
  216. this.m_errornum = 0;
  217. LogHelper.WriteLog(LogType.ServiceLog, "Service is paused");
  218. }
  219. catch (Exception ce)
  220. {
  221. LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message);
  222. }
  223. }
  224. #endregion
  225. #region OnContinue
  226. /// <summary>
  227. /// 重载继续运行方法
  228. /// </summary>
  229. protected override void OnContinue()
  230. {
  231. try
  232. {
  233. this.m_timer.AutoReset = true;
  234. LogHelper.WriteLog(LogType.ServiceLog, "Service continue to run");
  235. }
  236. catch (Exception ce)
  237. {
  238. LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message);
  239. }
  240. }
  241. #endregion
  242. #region OnShutdown
  243. /// <summary>
  244. /// 重载关闭方法
  245. /// </summary>
  246. protected override void OnShutdown()
  247. {
  248. this.OnStop();
  249. }
  250. #endregion
  251. }
  252. }