[英]Windows Service runs but stops processing after two days
我看过很多关于这个问题的帖子但不符合我的标准。
话虽如此,这是我构建的第一个“需要”来执行大量逻辑的。 所有这些都可以工作几天,但在此之后逻辑停止工作,没有登录事件查看器,也没有发送电子邮件(例外)。
我想知道我的逻辑是不正确的......寻找建议和指针。
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
是我的服务中的一个继承自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。
相关问题 。
我真的不会对这些步骤带来的问题感到困扰,因为我的目标是专注于业务逻辑。 所以我决定使用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
可怕经历虽然我没有解决核心问题,但我提出了一个解决方案,并获得了一个Timers.Timer
工作系统。
请注意,未来的读者请不要使用 System.Timers.Timer
除非您准备添加“hack'ish”修复程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.