UnhandledException处理程序在.NET Windows服务程序、UnhandledException、Windows、NET

2023-09-03 00:02:37 作者:独波大侠°

是否有可能使用UnhandledException处理程序在Windows服务?

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

通常我会用,做记录,手机之家等,这部分增加了一个处理程序System.AppDomain.CurrentDomain.UnhandledException但据我所知,这并不实现什么赢得了一个定制的异常处理组件Windows服务,所以我结束了这种模式在我2(或4)服务的入口点:

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

有没有办法我的自定义异常处理组件可以处理这更好的,所以我没有填我的OnStart凌乱的异常处理管道?

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?

推荐答案

好吧,我已经做了一点研究这个了。 当您创建一个Windows服务在.NET中,创建一个类,从了System.ServiceProcess.ServiceBase继承(在VB中,这是隐藏在.Designer.vb文件)。然后,覆盖的OnStart和的onStop功能,将OnPause和OnContinue如果您选择。 这些方法从基类中调用,所以我做了一个小戳与反射周围。 的OnStart是通过这样的方法在了System.ServiceProcess.ServiceBase称为ServiceQueuedMainCallback调用。在我的机器的vesionSystem.ServiceProcess,版本= 2.0.0.0这样的反编译:

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

所以,因为Me.OnStart(参数)是从一个try catch块的尝试部分中调用我认为任何OnStart方法内发生的是由有效的发生都不是try catch块,因此任何异常包裹技术上无法处理的,因为他们在ServiceQueuedMainCallback尝试捕捉实际处理。所以CurrentDomain.UnhandledException实际上从未发生至少启动过程中。 其他3个入口点(的onStop,将OnPause和OnContinue)都从基类以类似的方式称为

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.

所以,我认为这可以解释为什么我的异常处理组件不能赶上UnhandledException的启动和停止,但我不知道这是否可以解释为什么定时器,其设置在OnStart中不能引起UnhandledException当他们开火。

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.