Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.Net Windows服务中未处理的异常处理程序_.net_Vb.net_Exception Handling_Windows Services - Fatal编程技术网

.Net Windows服务中未处理的异常处理程序

.Net Windows服务中未处理的异常处理程序,.net,vb.net,exception-handling,windows-services,.net,Vb.net,Exception Handling,Windows Services,是否可以在Windows服务中使用未处理的异常处理程序 通常情况下,我会使用一个定制的异常处理组件来进行日志记录、呼叫总部等操作。该组件向System.AppDomain.CurrentDomain.UnhandledException添加了一个处理程序,但据我所知,这并不能实现赢得Windows服务的目的,因此我在我的2(或4)个服务入口点中使用了此模式: Protected Overrides Sub OnStart(ByVal args() As String)

是否可以在Windows服务中使用未处理的异常处理程序

通常情况下,我会使用一个定制的异常处理组件来进行日志记录、呼叫总部等操作。该组件向System.AppDomain.CurrentDomain.UnhandledException添加了一个处理程序,但据我所知,这并不能实现赢得Windows服务的目的,因此我在我的2(或4)个服务入口点中使用了此模式:


    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

我的自定义异常处理组件是否有更好的方法来处理此问题,这样我就不必在启动时使用混乱的异常处理管道了?

您可以订阅。如果您有一个消息循环,您可以绑定到。

好的,我现在对此做了更多的研究。 在.Net中创建windows服务时,将创建从System.ServiceProcess.ServiceBase继承的类(在VB中,该类隐藏在.Designer.VB文件中)。然后,您可以覆盖OnStart和OnStop函数,如果选择,还可以覆盖OnPause和OnContinue。 这些方法是从基类内部调用的,所以我对reflector做了一些研究。 OnStart由System.ServiceProcess.ServiceBase中名为ServiceQueuedMainCallback的方法调用。我机器上的vesion“System.ServiceProcess,Version=2.0.0.0”反编译如下:


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(args)是从Try-Catch块的Try部分调用的,所以我假设OnStart方法中发生的任何事情都会被该Try-Catch块有效地包装,因此发生的任何异常在技术上都不会未经处理,因为它们实际上是在ServiceQueuedMainCallback Try-Catch中处理的。因此,至少在启动例程期间,CurrentDomain.UnhandledException实际上从未发生过。 其他3个入口点(OnStop、OnPause和OnContinue)都以类似的方式从基类调用


所以我“认为”这就解释了为什么我的异常处理组件不能在启动和停止时捕获未处理的异常,但我不确定这是否解释了为什么在OnStart中设置的计时器在启动时不能导致未处理的异常

我发现服务中未处理的异常事件确实会在其他线程中接收异常,只要您很早就连接好事件处理程序。我将其连接到服务构造函数中,而不是OnStart()方法中。这也从OnStart()中删除了丑陋的异常管道。鉴于我对你的回复(几年前)被删除,我想我应该将其作为一条评论放回去,这是它一直以来应该做的:谢谢你的回答Garo,但正如我在最初的问题中所说,我们的异常处理程序确实向System.AppDomain.CurrentDomain.UnhandledException添加了一个处理程序。在Windows服务的上下文中使用它时,它根本不起作用。