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.
Scott Sep 12 '08 at 7:30 2008-09-12 07:30
source share