Global exception handler when creating a windows service?

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
 
D

DeveloperX

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);

}
 
O

oscar.acostamontesde

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top