Home All Groups Group Topic Archive Search About

Global exception handler when creating a windows service?

Author
6 Mar 2007 12:22 PM
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

Author
6 Mar 2007 12:42 PM
DeveloperX
On 6 Mar, 12:22, Simon Harvey <notha***@hotmail.com> wrote:
Show quote
> 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);

}
Author
7 Mar 2007 1:30 PM
oscar.acostamontesde@googlemail.com
Show quote
On Mar 6, 1:42 pm, "DeveloperX" <nntp***@operamail.com> wrote:
> On 6 Mar, 12:22, Simon Harvey <notha***@hotmail.com> wrote:
>
>
>
> > 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);
>
> }

Also you can use try/catch block in Main() method.
Best regards
Oscar Acosta
Author
8 Mar 2007 2:35 PM
Simon Harvey
oscar.acostamonte***@googlemail.com wrote:
Show quote
> On Mar 6, 1:42 pm, "DeveloperX" <nntp***@operamail.com> wrote:
>> On 6 Mar, 12:22, Simon Harvey <notha***@hotmail.com> wrote:
>>
>>
>>
>>> 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);
>>
>> }
>
> Also you can use try/catch block in Main() method.
> Best regards
> Oscar Acosta
>

Many thanks

Simon

AddThis Social Bookmark Button