【发布时间】:2010-09-08 16:00:36
【问题描述】:
是否可以在 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 an exit code here as well, just in case.
System.Environment.Exit(-1)
End Try
End Sub
我的自定义异常处理组件有什么方法可以更好地处理这个问题,这样我就不必在 OnStart 上填满杂乱的异常处理管道了吗?
【问题讨论】:
标签: .net vb.net exception-handling windows-services