开发者

Catching all exceptions in windows service C#

开发者 https://www.devze.com 2023-02-19 20:59 出处:网络
Is there a way i can catch all sort of exceptions in windows services. It is like i have a service which i should keep ru开发者_Python百科nning for ever and now if some connecting database or third pa

Is there a way i can catch all sort of exceptions in windows services. It is like i have a service which i should keep ru开发者_Python百科nning for ever and now if some connecting database or third party server dies for a moment i dont want my service to stop. I want that timers should give exceptions and after some time when server are up they should be working but right now exceptionn coming brings down my window service.

PS - I have read abt unhandles exception handler but i am not sure if that would stop from getting service crashed.I also have try catch block all over to catch generic exception (elapsed event handler).


No, there isn't. You can use UnhandledException event to generate debug info before your application crashes.

But creating your own exception handling shouldn't be that hard. All services has an entry point (Service.OnStart) where you launch your own thread/timer. Wrap all code in that thread/timer with a try/catch. That should catch all exceptions.

There is an exception though. Any threads/timers created in your code could still crash your application. You got to wrap all timer/thread callbacks with try/catch too.


Hi I got my application working by swallowing exception as it happens in .net framewroek 1.1, Below is the setting i added in app.config file.

<configuration> 
 <runtime> 
  <legacyUnhandledExceptionPolicy enabled="1" /> 
  </runtime> 
</configuration> 
0

精彩评论

暂无评论...
验证码 换一张
取 消