简体   繁体   中英

Windows service performs differently after reboot

I may have a problem in understating the behaviour of windows services or the life itself.

THE PROBLEM:

The service stopped unexpedetly and no recovery actions fired despite being set. The service stopped after ServiceHelper .ChangeStartMode method call

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

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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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