123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- 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;
- using EMIS.CommonLogic.SystemServices;
- using Senparc.Weixin.TenPay.V3;
- using EMIS.Web.Controls;
- 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<EMIS.CommonLogic.DALModule>();
- builder.RegisterModule<EMIS.CommonLogic.ServiceModule>();
- 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
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器
- /// 修改此方法的内容。
- /// </summary>
- 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
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- #endregion
- #region OnStart
- /// <summary>
- /// 设置具体的操作,以便服务可以执行它的工作。
- /// </summary>
- 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
- /// <summary>
- /// 时间器运行方法,调用ServiceWrapper
- /// </summary>
- /// <param name="source"></param>
- /// <param name="e"></param>
- protected void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
- {
- if (this.isRunning)
- {
- return;
- }
- this.isRunning = true;
- try
- {
- if (this.m_errornum < 100000)
- {
- try
- {
- LogHelper.WriteLog(LogType.ServiceLog, "----开始根据微信订单同步缴费状态-----");
- using (var scop = Program.AutofacContainer.BeginLifetimeScope())
- {
- IWechatPayServices WechatPayServices = scop.Resolve<IWechatPayServices>();
- var wecharPayList = WechatPayServices.SearchWeChatPayList();
- List<OrderQueryResult> orderList = new List<OrderQueryResult>();
- foreach (var wecharPay in wecharPayList)
- {
- OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID);
- orderList.Add(query);
- }
- WechatPayServices.UpdateWeChatPay(orderList);
- }
- LogHelper.WriteLog(LogType.ServiceLog, "----结束根据微信订单同步缴费状态-----");
- }
- catch (System.Exception err)
- {
- LogHelper.WriteLog(LogType.ServiceLog, "----根据微信订单同步缴费状态处理异常-----");
- LogHelper.WriteLog(LogType.ServiceLog, err.Message);
- //result = false;
- }
- ////动态创建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
- /// <summary>
- /// 停止此服务。
- /// </summary>
- 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
- /// <summary>
- /// 重载暂停运行方法
- /// </summary>
- 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
- /// <summary>
- /// 重载继续运行方法
- /// </summary>
- 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
- /// <summary>
- /// 重载关闭方法
- /// </summary>
- protected override void OnShutdown()
- {
- this.OnStop();
- }
- #endregion
- }
- }
|