unhandled exceptions in a windows service

青春壹個敷衍的年華 提交于 2019-12-07 17:40:55

问题


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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/10609443/unhandled-exceptions-in-a-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!