UnhandledException handler in .Net Windows service

Can I use the UnhandledException handler in a Windows service?

Normally I would use a custom built-in exception handling component that logs a log, home phone, etc. This component adds a handler to System.AppDomain.CurrentDomain.UnhandledException, but as far as I can tell about it, nothing can be achieved from the Windows service so I get this template at my 2 (or 4) service entry points:

Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Try MyServiceComponent.Start() Catch ex As Exception 'call into our exception handler MyExceptionHandlingComponent.ManuallyHandleException (ex) 'zero is the default ExitCode for a successfull exit, so if we set it to non-zero ExitCode = -1 'So, we use Environment.Exit, it seems to be the most appropriate thing to use 'we pass an exit code here as well, just in case. System.Environment.Exit(-1) End Try End Sub 

Is there a way that my custom exception handling component can handle this better, so I don't need to fill my OnStart with dirty exception handling?

+25
exception-handling windows-services
Sep 12 '08 at 4:06
source share
2 answers

Well, Ive done a bit more research on this now. When you create a Windows service in .Net, you create a class that inherits from System.ServiceProcess.ServiceBase (in VB this is hidden in the .Designer.vb file). Then you override the OnStart and OnStop functions, as well as OnPause and OnContinue, if you want. These methods are called from the base class, so I thought a little about the reflector. OnStart is called by a method in System.ServiceProcess.ServiceBase called ServiceQueuedMainCallback. Vesion on my machine "System.ServiceProcess, Version = 2.0.0.0" decompiles as follows:

 Private Sub ServiceQueuedMainCallback(ByVal state As Object) Dim args As String() = DirectCast(state, String()) Try Me.OnStart(args) Me.WriteEventLogEntry(Res.GetString("StartSuccessful")) Me.status.checkPoint = 0 Me.status.waitHint = 0 Me.status.currentState = 4 Catch exception As Exception Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { exception.ToString }), EventLogEntryType.Error) Me.status.currentState = 1 Catch obj1 As Object Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { String.Empty }), EventLogEntryType.Error) Me.status.currentState = 1 End Try Me.startCompletedSignal.Set End Sub
Private Sub ServiceQueuedMainCallback(ByVal state As Object) Dim args As String() = DirectCast(state, String()) Try Me.OnStart(args) Me.WriteEventLogEntry(Res.GetString("StartSuccessful")) Me.status.checkPoint = 0 Me.status.waitHint = 0 Me.status.currentState = 4 Catch exception As Exception Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { exception.ToString }), EventLogEntryType.Error) Me.status.currentState = 1 Catch obj1 As Object Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { String.Empty }), EventLogEntryType.Error) Me.status.currentState = 1 End Try Me.startCompletedSignal.Set End Sub 

So, since Me.OnStart (args) is called from the Try part in the Try Catch block, I assume that everything that happens inside the OnStart method effectively ends with a Try Catch block, and therefore any exceptions that are raised are not technically unhandled, since they are actually processed in ServiceQueuedMainCallback Try Catch. Thus, CurrentDomain.UnhandledException never occurs, at least during startup. The remaining 3 entry points (OnStop, OnPause, and OnContinue) are called from the base class in the same way.

So, I think this explains why my exception handling component cannot remove UnhandledException when starting and stopping, but I'm not sure if it explains why the timers that are configured on OnStart can throw UnhandledException when they start.

+15
Sep 12 '08 at 7:30
source share

You can subscribe to the AppDomain.UnhandledException event . If you have a message loop, you can associate with the Application.ThreadException event .

+2
Sep 12 '08 at 6:25
source share



All Articles