Program.cs 8.0 KB

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