Global exception handler when creating a windows service?

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi all,

What is the best way to catch any and all exceptions when creating a
windows service application? Is there some sort of global exception
event I can use, or is it possible to surround the

ServiceBase.Run(ServicesToRun);

line with a try catch? Would that work?

Also, when an exception occurs within a service, does it automatically
shut down?

Thanks to anyone who can advise

Kindest Regards

Simon
 
Hi all,

What is the best way to catch any and all exceptions when creating a
windows service application? Is there some sort of global exception
event I can use, or is it possible to surround the

ServiceBase.Run(ServicesToRun);

line with a try catch? Would that work?

Also, when an exception occurs within a service, does it automatically
shut down?

Thanks to anyone who can advise

Kindest Regards

Simon

When you start use:

AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

That hooks up to my exception handler:

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs pArgs)
{
PublishException((Exception)pArgs.ExceptionObject);

}
 
When you start use:

AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

That hooks up to my exception handler:

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs pArgs)
{
PublishException((Exception)pArgs.ExceptionObject);

}

Also you can use try/catch block in Main() method.
Best regards
Oscar Acosta
 
Back
Top