Windows服务中的未处理的异常 [英] unhandled exceptions in a windows service

查看:426
本文介绍了Windows服务中的未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了运行多个任务的.net c#windows服务。我在其中包含异常处理,但我想设置一个全局处理程序来捕获Windows服务中的未处理异常,我该怎么做?

I have created a .net c# windows service that runs multiple tasks. I included exception handling in it but I would like to set up a global handler to catch unhandled exceptions in the windows service, how can I do this?

推荐答案


我在其中包含异常处理,但是我想设置一个全局处理程序来捕获Windows服务中的未处理的异常

I included exception handling in it but I would like to set up a global handler to catch unhandled exceptions in the windows service

可以使用 AppDomain.UnhandledException 。然而,在您的情况下,您对您的服务的完整呼叫可以包裹在try / catch块中。由于您尚未提供有关此未处理异常的计划的详细信息,因此您认为在这种情况下您正确的路径是让服务崩溃

You could use AppDomain.UnhandledException. In your case however, your entire call to your service can be wrapped in a try/catch block. Since you haven't provided details for what you plan to do with this unhandled exception, your correct path I think in this case is to let the service crash.

try
{
   MainCallToYourService();
}
catch (Exception)
{
   //it's probably too late to do anything useful here, try to log and die
}

请记住,这样做的问题是,在很多情况下,您的应用程序的状态在此之前被破坏事件提高。最好的办法是尝试登录并退出。

Keep in mind though that the problem in doing this is that in a lot of cases, your application's state is corrupt by the time this event is raised. Your best bet is to try and log it and get out.

这篇关于Windows服务中的未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆