UnhandledException handler in a .Net Windows Service

13,557

Solution 1

Ok, I’ve done a little more research into 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). You then override the OnStart and OnStop function, and OnPause and OnContinue if you choose to. These methods are invoked from within the base class so I did a little poking around with reflector. OnStart 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:


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 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. The other 3 entry points (OnStop, OnPause and OnContinue) are all called from the base class in a similar way.

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.

Solution 2

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

Share:
13,557

Related videos on Youtube

Scott
Author by

Scott

By Day: Head Developer nah, sounds too naff Lead Developer hmmm still a bit naff Senior Developer makes me sound old.... Ninja... I can't even finish that one with a straight face its so 2008. I'm some guy that's been at Redgum since pretty much the get-go, we write software that others have deemed to be too difficult and generally fix projects that have gone off the rails. By Night: I spend time with my family and there's a pretty good chance my kid really is cuter than yours.

Updated on October 01, 2020

Comments

  • Scott
    Scott over 3 years

    Is it possible to use an UnhandledException Handler in a Windows Service?

    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:

    
        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 my Custom Exception Handling component can deal with this better so I don't have to fill my OnStart with messy exception handling plumbing?

  • Rama
    Rama about 14 years
    I've found that the UnhandledException event in a service does receive exceptions in other threads, as long as you wire up the event handler very early on. I wired it up in the service constructor rather than the OnStart() method. This also removes the ugly exception plumbing from your OnStart().
  • Scott
    Scott almost 11 years
    given my response to you was deleted (some years ago) I thought I'd put it back as a comment, which is what it should have been all along: Thanks for the answer Garo but as I said in my original question, our Exception handler does add a handler to System.AppDomain.CurrentDomain.UnhandledException it simply doesn't work when using it in the context of a Windows Service.