c#-Windows服务运行,但两天后停止处理

我见过很多关于此问题的帖子,但似乎不符合我的标准.话虽如此,这是我构建的第一个“需要”执行大量逻辑的程序.一切正常工作了几天,但是在那之后逻辑停止工作,没有在事件查看器中登录,也没有触发电子邮件(异常).我想...

我见过很多关于此问题的帖子,但似乎不符合我的标准.

话虽如此,这是我构建的第一个“需要”执行大量逻辑的程序.一切正常工作了几天,但是在那之后逻辑停止工作,没有在事件查看器中登录,也没有触发电子邮件(异常).

我想知道我的逻辑是否正确…寻找建议和指示.

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();
            }
        }
    }
}

为了达到这一点,我用“ google”和“ SO’ed”来找到计时器的最佳用法…我也有一个简单的测试层,可以再现服务中的内容,我可以毫无例外地运行,但是这个让我发疯.

任何帮助,真正的赞赏!

//编辑

关于以下内容的一些解释:

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

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

一切正常工作了两天,但似乎停止了.我没有在shopify内达到请求限制…所以目前,我很茫然.

解决方法:

经过一会儿的抓挠和对MS Timer Bug的极大厌烦.

它不仅吞下了异常,而且计时器没有引发该事件!我的服务被挂起了,这特别困难,因为它也吞没了异常.

现在我确实走了用Threading.Timer实现的一半路线

>不要使用System.Windows.Forms.Timer,因为它不起作用(这仅是有意义的).
>不要使用System.Threading.Timer,因为它不起作用,请改用System.Timers.Timer.
>不要使用System.Timers.Timer,因为它不起作用,请改用System.Threading.Timer.

Related question.

我真的不会为这些步骤带来的问题所困扰,因为我的目标是专注于业务逻辑.因此,我决定使用Quartz.Net.

它使我的生活变得轻松多了,并且看起来运行良好!价值45-60分钟的工作.

基本设置:

1,Nuget>石英

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

3,在相应的文件夹中添加了OrderJob.cs和OrderJobSchedule.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<OrderJob>().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<OrderJob>().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虽然我没有解决核心问题,但我想出了一个解决方案,并安装了无错误的工作系统.

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

本文标题为:c#-Windows服务运行,但两天后停止处理

基础教程推荐