Problem catching all exceptions in a windows service


S

Simon

Hi all,

I'm having a problem creating a global exception handler in a windows
service that I'm making. Another poster suggested that the way to do
this was to use the CurrentDomain.UnhandledException event to catch and
deal with any exception that crops up. The problem is, when I try and
deliberately throw an exception in my services OnStart event handler,
nothing seems to happen. By that I mean, the
CurrentDomain_UnhandledException handler that I've created never seems
to be called.

Can anyone advise me if I'm doing this correctly? I've included the code
below. I just set up the event handler in the main method...

Thanks to anyone who can offer any help

Kindest Regards

Simon

static class Program {

static void Main() {
ServiceBase[] servicesToRun;
servicesToRun = new ServiceBase[] { new REMUSSchedulerService() };

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

ServiceBase.Run(servicesToRun);
}

static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in
UnhandledException event handler: " + e.ExceptionObject.ToString());
}
}
 
Ad

Advertisements

B

Brian Gideon

Hi all,

I'm having a problem creating a global exception handler in a windows
service that I'm making. Another poster suggested that the way to do
this was to use the CurrentDomain.UnhandledException event to catch and
deal with any exception that crops up. The problem is, when I try and
deliberately throw an exception in my services OnStart event handler,
nothing seems to happen. By that I mean, the
CurrentDomain_UnhandledException handler that I've created never seems
to be called.

Can anyone advise me if I'm doing this correctly? I've included the code
below. I just set up the event handler in the main method...

Thanks to anyone who can offer any help

Kindest Regards

Simon

static class Program {

static void Main() {
ServiceBase[] servicesToRun;
servicesToRun = new ServiceBase[] { new REMUSSchedulerService() };

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

ServiceBase.Run(servicesToRun);
}

static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
TraceHelper.WriteLine(TraceLevel.Info, "Exception detected in
UnhandledException event handler: " + e.ExceptionObject.ToString());
}

That's not the way I do it. The OnStart method must complete within a
certain amount of time (I think 30 seconds) or the SCM will think
something is wrong. It must not throw exceptions or the service will
not start. Now, typically, the OnStart method just gets something
going asynchronously like a timer or thread and returns immediately.
The timer callback/event or thread is the place I put a "global"
exception handler.
 

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