Windows服务运行但在两天后停止处理

我看过很多关于这个问题的post但不符合我的标准。

话虽如此,这是我构建的第一个“需要”来执行大量逻辑的。 所有这些都可以工作几天,但在此之后逻辑停止工作,没有登录事件查看器,也没有发送电子邮件(例外)。

我想知道我的逻辑是不正确的……寻找建议和指针。

public partial class QuayService : ServiceBase { private System.Timers.Timer m_mainTimer; private bool m_timerTaskSuccess; private Email _email; public QuayService() { InitializeComponent(); _email = new Email(); } protected override void OnStart(string[] args) { try { // Create and start a timer. m_mainTimer = new System.Timers.Timer(); m_mainTimer.Interval = 5000; // every 5 seconds m_mainTimer.Elapsed += m_mainTimer_Elapsed; m_mainTimer.AutoReset = false; // makes it fire only once m_mainTimer.Start(); // Start m_timerTaskSuccess = false; _email.SendEmail("Quay", "(Shopify)Quay Service Started", "(Shopify)Quay Service Successfuly Started"); EventLog.WriteEntry("(Shopify)Quay Service Started..."); } catch (Exception ex) { // Log/Send Email _email.SendEmail("Quay", "Error starting (Shopify)Quay Service", ex.Message + " " + ex.InnerException.Message); EventLog.WriteEntry("Error starting (Shopify)Quay service and timer..." + ex.Message + " " + ex.InnerException.Message); } } protected override void OnStop() { try { // Service stopped. Also stop the timer. m_mainTimer.Stop(); m_mainTimer.Dispose(); m_mainTimer = null; _email.SendEmail("Quay", "(Shopify)Quay Service stopped", "(Shopify)Quay Service Successfuly Stopped"); EventLog.WriteEntry("(Shopify)Quay Service stopped..."); } catch (Exception ex) { _email.SendEmail("Quay", "Error stopping (Shopify)Quay Service", ex.Message + " " + ex.InnerException.Message); // Log/Send Email EventLog.WriteEntry("Error stopping (Shopify)Quay timer and service..." + ex.Message + " " + ex.InnerException.Message); } } void m_mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { var orderUoW = new OrderUoW(); orderUoW.Create(); m_timerTaskSuccess = true; } catch (Exception ex) { //Error with timer elapsed m_timerTaskSuccess = false; _email.SendEmail("Quay", "Error creating (Shopify)Quay order(s)", ex.Message + " " + ex.InnerException.Message); EventLog.WriteEntry("Error creating (Shopify)Quay order(Time elapsed event)..." + ex.Message + " " + ex.InnerException.Message); } finally { if (m_timerTaskSuccess) { m_mainTimer.Start(); } } } } 

为了达到这一点,我“googled”和“SO’ed”找到计时器的最佳用途…我还有一个天真的测试层,它可以重现服务中的内容,我可以毫无例外地完成这项任务。这个让我疯了。

任何帮助真的很感激!

//编辑

一点点解释:

 var orderUoW = new OrderUoW(); orderUoW.Create(); 

OrderUoW是我的服务中的一个类,它inheritance自BatchOrder ,它负责许多操作,包括连接到两个数据库和轮询shopify API … OrderUow纯粹是为了与业务层分离,而是提供对“x”方法的访问。

所有工作都完美无缺地工作了两天,但似乎停了下来。 我没有在shopify中达到要求限制……所以此刻,我不知所措。

经过许多时刻的磨损和MS Timer Bug的极度烦恼。

它不仅吞下exception,定时器也没有引发事件! 我的服务暂停了,这特别困难,因为它也吞噬了exception。

现在我确实沿着Threading.Timer实现了一半的实现

  1. 不要使用System.Windows.Forms.Timer,因为它不起作用(这只是有意义的)。

  2. 不要使用System.Threading.Timer,因为它不起作用,请改用System.Timers.Timer。

  3. 不要使用System.Timers.Timer,因为它不起作用,请改用System.Threading.Timer。

相关问题 。

我真的不能为这些步骤带来的问题烦恼,因为我的目标是专注于业务逻辑。 所以我决定使用Quartz.Net 。

它让我的生活更轻松,似乎完美无缺! 45-60分钟的工作。

基本设置:

1,Nuget>石英

2,新文件夹> QuartzComponents>工作和时间表

在此处输入图像描述

3,在各自的文件夹中添加了OrderJob.csOrderJobSchedule.cs

OrderJob.cs逻辑:

  public sealed class OrderJob : IJob { public void Execute(IJobExecutionContext context) { var orderUoW = new OrderUoW(); orderUoW.Create(); } } 

请注意,它创建了我的UoW的实例?!? 它会在每次传球时击中的逻辑。

OrderJobSchedule.cs逻辑:

  public sealed class OrderJobSchedule { public void Start() { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); IJobDetail job = JobBuilder.Create().Build(); ITrigger trigger = TriggerBuilder.Create() .WithSimpleSchedule(a => a.WithIntervalInSeconds(15).RepeatForever()) .Build(); scheduler.ScheduleJob(job, trigger); } public void Stop() { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Shutdown(); } } 

这里有很多魔力,但强调:

 JobBuilder.Create().Build(); a.WithIntervalInSeconds(15).RepeatForever() 

现在,我们需要为服务的“胆量”添加逻辑:

 public partial class QuayService : ServiceBase { OrderJobSchedule scheduler; public QuayService() { InitializeComponent(); } protected override void OnStart(string[] args) { try { scheduler = new OrderJobSchedule(); scheduler.Start(); SendEmail("(Shopify)Quay Service Started", "(Shopify)Quay Service Successfuly Started"); } catch (Exception ex) { ProcessException(ex, "Error starting (Shopify)Quay Service"); EventLog.WriteEntry("Error starting (Shopify)Quay service and timer..." + ex.Message); } } protected override void OnStop() { try { if (scheduler != null) { scheduler.Stop(); } SendEmail("(Shopify)Quay Service stopped", "(Shopify)Quay Service Successfuly Stopped"); } catch (Exception ex) { ProcessException(ex, "Error stopping (Shopify)Quay Service"); EventLog.WriteEntry("Error stopping (Shopify)Quay timer and service..." + ex.Message); } } private void SendEmail(string subject, string body) { new Email().SendErrorEmail("Quay", subject, body); } private void ProcessException(Exception ex, string customMessage) { var innerException = ""; if (ex.InnerException != null) innerException = (!string.IsNullOrWhiteSpace(ex.InnerException.Message)) ? ex.InnerException.Message : ""; new Email().SendErrorEmail("Quay", customMessage, ex.Message + " " + innerException); } } 

很容易设置和解决我与Timers.Timer可怕经历虽然我没有解决核心问题,但我提出了一个解决方案,并获得了一个Timers.Timer工作系统。

请注意,未来的读者请不要使用 System.Timers.Timer除非您准备添加“hack’ish”修复程序。