<p>Is it possible to use an UnhandledException Handler in a Windows Service?</p>

<p>Normally I would use a custom built Exception Handling Component that does logging, phone home, etc. This component adds a handler to System.AppDomain.CurrentDomain.UnhandledException but as far as I can tell this doesn’t achieve anything win a Windows Service so I end up with this pattern in my 2 (or 4) Service entry points:</p>

<pre>
<code>
    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
</code>
</pre>

<p>Is there a way my Custom Exception Handling component can deal with this better so I don't have to fill my OnStart with messy exception handling plumbing?</p>

[{'answer_id': 58408, 'author': 'Garo Yeriazarian', 'author_id': 2655, 'author_profile': 'https://Stackoverflow.com/users/2655', 'pm_score': 2, 'selected': False, 'text': '<p>You can subscribe to the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" rel="nofollow noreferrer">AppDomain.UnhandledException event</a>.  If you have a message loop, you can tie to the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx" rel="nofollow noreferrer">Application.ThreadException event</a>.</p>\n'}, {'answer_id': 58450, 'author': 'Scott', 'author_id': 6042, 'author_profile': 'https://Stackoverflow.com/users/6042', 'pm_score': 5, 'selected': True, 'text': '<p>Ok, I’ve done  a little more research into this now.\nWhen 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). You then override the OnStart and OnStop function, and OnPause and OnContinue if you choose to. \nThese methods are invoked from within the base class so I did a little poking around with reflector.\nOnStart is invoked by a method in System.ServiceProcess.ServiceBase called ServiceQueuedMainCallback.  The vesion on my machine "System.ServiceProcess, Version=2.0.0.0" decompiles like this:</p>\n\n<pre>\n<code>\nPrivate Sub ServiceQueuedMainCallback(ByVal state As Object)\n    Dim args As String() = DirectCast(state, String())\n    Try \n        Me.OnStart(args)\n        Me.WriteEventLogEntry(Res.GetString("StartSuccessful"))\n        Me.status.checkPoint = 0\n        Me.status.waitHint = 0\n        Me.status.currentState = 4\n    Catch exception As Exception\n        Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { exception.ToString }), EventLogEntryType.Error)\n        Me.status.currentState = 1\n    Catch obj1 As Object\n        Me.WriteEventLogEntry(Res.GetString("StartFailed", New Object() { String.Empty }), EventLogEntryType.Error)\n        Me.status.currentState = 1\n    End Try\n    Me.startCompletedSignal.Set\nEnd Sub\n</code>\n</pre>\n\n<p>So because Me.OnStart(args) is called from within the Try portion of a Try Catch block I assume that anything that happens within the OnStart method is effectively wrapped by that Try Catch block and therefore any exceptions that occur aren\'t technically unhandled as they are actually handled in the ServiceQueuedMainCallback Try Catch. So CurrentDomain.UnhandledException never actually happens at least during the startup routine. \nThe other 3 entry points (OnStop, OnPause and OnContinue) are all called from the base class in a similar way.</p>\n\n<p>So I ‘think’ that explains why my Exception Handling component can’t catch UnhandledException on Start and Stop, but I’m not sure if it explains why timers that are setup in OnStart can’t cause an UnhandledException when they fire. </p>\n'}]
['https://Stackoverflow.com/questions/58280', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6042/']