views:

291

answers:

5
protected override void OnStart(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Thread.Sleep(10000);

    throw new Exception();
}

void CurrentDomain_UnhandledException(object sender,
                                      UnhandledExceptionEventArgs e)
{
}

I attached a debugger to the above code in my windows service, setting a breakpoint in CurrentDomain_UnhandledException, but it was never hit. The exception pops up saying that it is unhandled, and then the service stops. I even tried putting some code in the event handler, in case it was getting optimized away.

Is this not the proper way to set up unhandled exception handling in a windows service?

+2  A: 

Just curious, what are you trying to accomplish: avoiding service crashing, or reporting errors?

For reporting, I think your best bet is to add top-level try/catch statements. You can try to log them to the Windows event log and/or a log file.

You can also set the ExitCode property to a non-zero value until you successfully stop the service. If the system administrator starts your service from the Services control panel, and your service stops suddenly with a non-zero exit code, Windows can show an error message with the description of the error.

Paul Williams
Debugging. I've tried catching all of the exceptions, but one is escaping me somehow. I figured this would be a more manageable solution.
Mike Pateras
Is the exception happening in OnStart or OnStop? Do you have any other information about the exception?
Paul Williams
A: 

You'll typically want to catch the ThreadException.

protected override void OnStart(string[] args)

{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // Runs the application.
    Application.Run(new ErrorHandlerForm());
}
Carra
What is the "Application" in that example? From what I've seen, Application usually refers to System.Windows.Forms.Application, which I do not have in a Windows Service.
Mike Pateras
A: 

So it seems like this can't be done. I'm open to further suggestions, though.

Mike Pateras
+1  A: 

In a Windows Service you do NOT want to be running much code in the OnStart method. All you want there is the code to launch your service thread and then return.

If you do that you can handle exceptions that happen in your service thread just fine.

e.g.

public static void Start()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);

    running = true;
    ThreadStart ts = new ThreadStart(ServiceThreadBody);
    thread = new Thread(ts);
    thread.Name = "ServiceThread";
    thread.Priority = ThreadPriority.BelowNormal;
    thread.Start();
}
Hightechrider
A: 

Know this thread is a bit old, but thought it would be useful to add some comments based on personal experience developing Windows services in .NET. The best approach is to avoid developing under the Service Control Manager as much as you can - for this you need a simple harness that mimics the way services get started - something that can create an instance of your service class (that you already derived from ServiceBase) and call your OnStart, OnStop etc methods. This harness can be a console app or a Windows app as you desire.

This is pretty much the only way I have found of debugging service start-up issues in .NET - the interaction between your code, Visual Studio and the real Service Control Manager just makes the process impossible otherwise.

HTH.

SteveWilkinson