是否可以在Windows服务中使用UnhandledException处理程序?
通常我会使用自定义构建的异常处理组件来执行日志记录,电话回家等.这个组件为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 …Run Code Online (Sandbox Code Playgroud)