C# Service and Throwing Exceptions

B

Brian Pelton

Hi All...

Why would exceptions thrown from a service process not be caught or stop
the service?

I have a fairly simple service written that has a timer and a timer
elapsed event.

Throwing an exception from Main() does stop the service and it does
trigger log4net to send an email to me with the exception. However,
throwing an exception from timer_elapsed() doesn't do much - it
basically exits the method at the point where the exception is thrown.
But, in a few seconds, the timer elapsed event fires again and basically
the service continues as it never had an exception.

Is this normal? How can I detect that an exception was thrown? Do i
have to put my try block at the timer_elapsed method? Will exceptions
never bubble up to my Main() method?

Thanks!
Brian





Here is the timer_elapsed method:

private void ServiceHeartbeat_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
log.Info("Heartbeat.");

if (DateTime.Now.Second % 2 == 0)
{
log.Info("Throwing Exception...");
throw new ApplicationException("Really Bad Problem!");
}

//if ApplicationException was thrown, this does not get logged.
log.Info("Still In Heartbeat.");
}


Here is the Main() method:

static void Main()
{
//init log4net
if (File.Exists(Global.ServiceSettings.LoggingConfigurationFile))
{
XmlConfigurator.Configure(new
FileInfo(Global.ServiceSettings.LoggingConfigurationFile));
log.Info("Logging Initialized.");
}

//init service
AonService svc = new AonService(Global.ServiceSettings);

try
{
System.ServiceProcess.ServiceBase.Run(svc);
}
catch (Exception ex)
{
log.Fatal(ex.ToString());
}

}
 
R

Rob Schieber

Brian said:
Hi All...

Why would exceptions thrown from a service process not be caught or stop
the service?

I have a fairly simple service written that has a timer and a timer
elapsed event.

Throwing an exception from Main() does stop the service and it does
trigger log4net to send an email to me with the exception. However,
throwing an exception from timer_elapsed() doesn't do much - it
basically exits the method at the point where the exception is thrown.
But, in a few seconds, the timer elapsed event fires again and basically
the service continues as it never had an exception.

Is this normal? How can I detect that an exception was thrown? Do i
have to put my try block at the timer_elapsed method? Will exceptions
never bubble up to my Main() method?

Thanks!
Brian





Here is the timer_elapsed method:

private void ServiceHeartbeat_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
log.Info("Heartbeat.");

if (DateTime.Now.Second % 2 == 0)
{
log.Info("Throwing Exception...");
throw new ApplicationException("Really Bad Problem!");
}

//if ApplicationException was thrown, this does not get logged.
log.Info("Still In Heartbeat.");
}


Here is the Main() method:

static void Main()
{
//init log4net
if (File.Exists(Global.ServiceSettings.LoggingConfigurationFile))
{
XmlConfigurator.Configure(new
FileInfo(Global.ServiceSettings.LoggingConfigurationFile));
log.Info("Logging Initialized.");
}

//init service
AonService svc = new AonService(Global.ServiceSettings);

try
{
System.ServiceProcess.ServiceBase.Run(svc);
}
catch (Exception ex)
{
log.Fatal(ex.ToString());
}

}
Hi Brian,

I believe that your timer events are occuring on a different thread, and
this is why you arent seeing anything, the thread just dies. If you want
to terminate the program you need to throw the exception on the main
thread.
 
G

Guest

What timer are you using? You're thread callback is on a different process.
You could set a flag for your main thread... Remember to use Mutex as it
different process vs different thread.

/jhd
 

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