What is the proper way for a Windows service to fail?

前端 未结 5 1696
日久生厌
日久生厌 2020-12-05 02:17

I have inherited a Windows service written in C#. Under rare conditions it fails badly. However, it isn\'t at all clear how to fail well. Ross Bennett states the problem e

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 02:50

    You can get the proper ExitCode as described here. So the Windows Service Manager will give the right error text.

    My OnStart in ServiceBase looks like this:

    protected override void OnStart(string[] args)
    {
        try
        {
            DoStart();
        }
        catch (Exception exp)
        {
            Win32Exception w32ex = exp as Win32Exception;
            if (w32ex == null)
            {
                w32ex = exp.InnerException as Win32Exception;
            }
            if (w32ex != null)
            {
                ExitCode = w32ex.ErrorCode;
            }
            Stop();
        }
    }
    

提交回复
热议问题