简体   繁体   English

Windows服务在重新启动后执行不同

[英]Windows service performs differently after reboot

I may have a problem in understating the behaviour of windows services or the life itself. 我可能会低估Windows服务的行为或生活本身。

THE PROBLEM: 问题:

The service stopped unexpedetly and no recovery actions fired despite being set. 该服务异常停止,尽管已设置,但未执行任何恢复操作。 The service stopped after ServiceHelper .ChangeStartMode method call 服务调用ServiceHelper .ChangeStartMode方法后停止

try
{                       
    normalnekurwalogowanie(Constants.Values.service_name);
    ServiceController svc = new ServiceController(Constants.Values.service_name);
    if (svc != null)
    {                            
        ServiceHelper.ChangeStartMode(svc, (automatic ? ServiceStartMode.Automatic : ServiceStartMode.Manual));
        svc.Close();
    }
    else
        normalnekurwalogowanie("null");
}
catch (Exception ex)
{
    //Logger.Instance.Error("Error message: {0}\nError Stack Trace: {1}", new object[] { ex.Message, ex.StackTrace });
    normalnekurwalogowanie(ex.ToString());
}

In my log file there was an error Open Service Manager Error : Unable to open Service Manager 在我的日志文件中,有一个错误Open Service Manager ErrorUnable to open Service Manager

Now, few interesting facts: - As you can see the exception was caught and printed to the file yet the service stopped - The error occured only after reboot; 现在,有几个有趣的事实:-如您所见,捕获了异常并将其打印到文件中,但服务已停止-仅在重新启动后才发生错误; it doesn't occur after service installation before system reboot 服务安装后,在系统重新启动之前不会发生

THE SOLUTION: 解决方案:

After I removed reference to external Logger class (not written by me, I don't have the code) the problem disappeared. 删除对外部Logger类的引用(不是我写的,没有代码)后,问题消失了。 I don't know why. 我不知道为什么

THE QUESTIONS: 问题:

  • How can caught exception still crash the service (and in a way recovery actions are not performed)? 如何捕获异常仍会使服务崩溃(并以某种方式不执行恢复操作)?
  • How can code perform differently after reboot? 重新启动后代码如何表现不同? It goes thru exactly same sequence. 它通过完全相同的顺序。

Even if the external class may have impact on my code it wasn't called anywhere before the line that threw the exception. 即使外部类可能对我的代码产生影响,也不会在引发异常的行之前的任何地方调用它。 BTW the externall class used in winforms app works fine, in service before reboot works fine. 顺便说一句,在winforms应用程序中使用的externall类工作正常,在重新启动之前可以正常使用。

I will try to acquire external class code and update the question. 我将尝试获取外部类代码并更新问题。

How can caught exception still crash the service (and in a way recovery actions are not performed)? 如何捕获异常仍会使服务崩溃(并以某种方式不执行恢复操作)?

The original exception may not crash the service, but if you have a second exception in your first exception handler, the service will crash. 原始异常可能不会使服务崩溃,但是如果您的第一个异常处理程序中有第二个异常,则服务将崩溃。 I'd check if Logger.Instance.Error() is throwing an exception by putting a try/catch around it. 我会检查Logger.Instance.Error()是否通过在其周围放置try / catch引发异常。

How can code perform differently after reboot? 重新启动后代码如何表现不同? It goes thru exactly same sequence. 它通过完全相同的顺序。

While it might be the same sequence in your code, we don't know what residual state was left over on disk before the last crash. 尽管代码中的序列可能相同,但是我们不知道在上次崩溃之前磁盘上还剩下什么残留状态。 This might be the reason for the difference. 这可能是造成差异的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM