PC Review


Reply
Thread Tools Rate Thread

C# Service and Throwing Exceptions

 
 
Brian Pelton
Guest
Posts: n/a
 
      2nd Nov 2005
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());
}

}

 
Reply With Quote
 
 
 
 
Rob Schieber
Guest
Posts: n/a
 
      2nd Nov 2005
Brian Pelton wrote:
> 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.

--
Rob Schieber
 
Reply With Quote
 
=?Utf-8?B?RGhvbWluYXRvcg==?=
Guest
Posts: n/a
 
      5th Nov 2005
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

"Rob Schieber" wrote:

> Brian Pelton wrote:
> > 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.
>
> --
> Rob Schieber
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Throwing Exceptions Jonathan Wood Microsoft C# .NET 8 5th Jun 2008 03:24 AM
Re-Throwing Exceptions sternr Microsoft C# .NET 11 11th Jun 2006 08:41 AM
throwing exceptions from COM+ =?Utf-8?B?Z3V5?= Microsoft Dot NET 2 18th Oct 2005 08:56 AM
Throwing exceptions Mark Rae Microsoft C# .NET 11 17th Apr 2005 12:07 PM
Throwing Exceptions C Microsoft ASP .NET 2 6th Nov 2003 03:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:30 PM.