using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Reflection; using Autofac; using EMIS.CommonLogic; using System.Configuration; using EMIS.Utility; using Bowin.Common.Log; namespace EMIS.Services { public class Program : ServiceBase { private System.ComponentModel.Container components = null; private System.Timers.Timer m_timer; private int m_errornum = 0; //private Log _log = new Log(); private string sServiceName = ""; private bool isRunning = false; public static readonly Autofac.IContainer AutofacContainer; static Program() { var builder = new ContainerBuilder(); builder.RegisterModule(); builder.RegisterModule(); var _container = builder.Build(); AutofacContainer = _container; AutofacHelper.Container = _container; } public Program() { // 该调用是 Windows.Forms 组件设计器所必需的。 InitializeComponent(); } #region 进程的主入口点 static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; //System.Diagnostics.Debugger.Launch(); // 同一进程中可以运行多个用户服务。若要将 // 另一个服务添加到此进程,请更改下一行 // 以创建另一个服务对象。例如, // // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; // ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Program() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } #endregion #region InitializeComponent /// /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// private void InitializeComponent() { components = new System.ComponentModel.Container(); helpInitializeComponent(); } private void helpInitializeComponent() { //防止对设计视图的不可观察影响. GaoXT20090206 this.CanHandlePowerEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; sServiceName = ConfigurationManager.AppSettings["ServiceName"]; this.ServiceName = "BackEnd Service For " + sServiceName; } #endregion #region Dispose /// /// 清理所有正在使用的资源。 /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #endregion #region OnStart /// /// 设置具体的操作,以便服务可以执行它的工作。 /// protected override void OnStart(string[] args) { LogHelper.WriteLog(LogType.ServiceLog, "服务开始!"); //获取时间设置的缺省值 string timeInterVal = ConfigurationManager.AppSettings["Interval"]; //将缺省值设置间隔时间设为计时器的间隔时间 this.m_timer = new System.Timers.Timer(); if (timeInterVal.Length != 0) { this.m_timer.Interval = System.Convert.ToInt32(timeInterVal); } else { this.m_timer.Interval = 10000; //缺省为10秒 } this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimedEvent); this.m_timer.AutoReset = true; this.m_timer.Enabled = true; this.m_timer.Start(); } #endregion #region 时间器运行方法,调用ServiceWrapper /// /// 时间器运行方法,调用ServiceWrapper /// /// /// protected void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) { if (this.isRunning) { return; } this.isRunning = true; try { if (this.m_errornum < 100000) { //动态创建Wrapper,并将基本值赋予其属性 string sWrapper = ConfigurationManager.AppSettings["Wrapper"]; string[] sArray = sWrapper.Split(','); Assembly assem = Assembly.Load(sArray[0].ToString()); Type classType = assem.GetType(sArray[1].ToString()); EMIS.Services.MyEAPServiceWrapper.IServiceWrapper fss = (EMIS.Services.MyEAPServiceWrapper.IServiceWrapper)System.Activator.CreateInstance(classType); fss.IsDebug = System.Convert.ToBoolean(ConfigurationManager.AppSettings["IsDebug"]); bool returnValue = true; returnValue = fss.Execute(); //运行Wrapper if (!returnValue) this.m_errornum++; } else { LogHelper.WriteErrorLog(LogType.ServiceLog, "more than 100000 exceptions occurred when run ServiceWrapper, begin to stop service"); this.OnStop(); } } catch (Exception ce) { LogHelper.WriteErrorLog(LogType.ServiceLog, "Exception occur when run EAPService.OnTimedEvent,pls refer :" + ce.Message + "||" + ce.TargetSite + "||" + ce.StackTrace); this.m_errornum++; } finally { this.isRunning = false; } } #endregion #region OnStop /// /// 停止此服务。 /// protected override void OnStop() { try { this.m_timer.Enabled = false; this.m_timer.Stop(); this.m_timer.Close(); this.m_errornum = 0; LogHelper.WriteLog(LogType.ServiceLog, "机器服务【" + sServiceName + "】结束!"); } catch (Exception ce) { LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message); } } #endregion #region OnPause /// /// 重载暂停运行方法 /// protected override void OnPause() { try { this.m_timer.AutoReset = false; this.m_errornum = 0; LogHelper.WriteLog(LogType.ServiceLog, "Service is paused"); } catch (Exception ce) { LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message); } } #endregion #region OnContinue /// /// 重载继续运行方法 /// protected override void OnContinue() { try { this.m_timer.AutoReset = true; LogHelper.WriteLog(LogType.ServiceLog, "Service continue to run"); } catch (Exception ce) { LogHelper.WriteErrorLog(LogType.ServiceLog, ce.Message); } } #endregion #region OnShutdown /// /// 重载关闭方法 /// protected override void OnShutdown() { this.OnStop(); } #endregion } }