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
|