Home All Groups Group Topic Archive Search About

Unhandled Exception In Windows Service .Net 2.0

Author
11 Dec 2006 7:08 PM
twahl
Hi,
I need to know if the AppDomain.UnhandledException event can be handled
using a Windows Service.  I've create a Windows Service that uses the
Exception Handling Application Block and there are certain exceptoions that
I want bubbled up as unhandled exceptions.  I've used reflector to see what
is going on in the Framework and it looks like the exception is re-thrown so
I'm wondering what I am doing wrong?

Thanks for your help in advance,
Terry 

private unsafe void DeferredStop()
{
      fixed (NativeMethods.SERVICE_STATUS* service_statusRef1 = &this.status)
      {
            int num1 = this.status.currentState;
            this.status.checkPoint = 0;
            this.status.waitHint = 0;
            this.status.currentState = 3;
            NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
            try
            {
                  this.OnStop();
                  this.WriteEventLogEntry(Res.GetString("StopSuccessful"));
                  this.status.currentState = 1;
                  NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
                  if (this.isServiceHosted)
                  {
                        try
                        {
                              AppDomain.Unload(AppDomain.CurrentDomain);
                        }
                        catch (CannotUnloadAppDomainException exception1)
                        {

this.WriteEventLogEntry(Res.GetString("FailedToUnloadAppDomain", new object[]
{ AppDomain.CurrentDomain.FriendlyName, exception1.Message }),
EventLogEntryType.Error);
                        }
                  }
            }
            catch (Exception exception2)
            {
                  this.status.currentState = num1;
                  NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
object[] { exception2.ToString() }), EventLogEntryType.Error);
                  throw;
            }
            catch
            {
                  this.status.currentState = num1;
                  NativeMethods.SetServiceStatus(this.statusHandle,
service_statusRef1);
                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
object[] { string.Empty }), EventLogEntryType.Error);
                  throw;
            }
      }
}

Author
11 Dec 2006 7:31 PM
Gabriel Lozano-Morán
I know that the AppDomain.UnhandledException event will not be raised
always. See here for a lead:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.unhandledexceptionmode.aspx

Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net

Show quote
"twahl" <tw***@discussions.microsoft.com> wrote in message
news:C03FEF25-547D-4821-A464-646D5F8EF470@microsoft.com...
> Hi,
> I need to know if the AppDomain.UnhandledException event can be handled
> using a Windows Service.  I've create a Windows Service that uses the
> Exception Handling Application Block and there are certain exceptoions
> that
> I want bubbled up as unhandled exceptions.  I've used reflector to see
> what
> is going on in the Framework and it looks like the exception is re-thrown
> so
> I'm wondering what I am doing wrong?
>
> Thanks for your help in advance,
> Terry
>
> private unsafe void DeferredStop()
> {
>      fixed (NativeMethods.SERVICE_STATUS* service_statusRef1 =
> &this.status)
>      {
>            int num1 = this.status.currentState;
>            this.status.checkPoint = 0;
>            this.status.waitHint = 0;
>            this.status.currentState = 3;
>            NativeMethods.SetServiceStatus(this.statusHandle,
> service_statusRef1);
>            try
>            {
>                  this.OnStop();
>                  this.WriteEventLogEntry(Res.GetString("StopSuccessful"));
>                  this.status.currentState = 1;
>                  NativeMethods.SetServiceStatus(this.statusHandle,
> service_statusRef1);
>                  if (this.isServiceHosted)
>                  {
>                        try
>                        {
>                              AppDomain.Unload(AppDomain.CurrentDomain);
>                        }
>                        catch (CannotUnloadAppDomainException exception1)
>                        {
>
> this.WriteEventLogEntry(Res.GetString("FailedToUnloadAppDomain", new
> object[]
> { AppDomain.CurrentDomain.FriendlyName, exception1.Message }),
> EventLogEntryType.Error);
>                        }
>                  }
>            }
>            catch (Exception exception2)
>            {
>                  this.status.currentState = num1;
>                  NativeMethods.SetServiceStatus(this.statusHandle,
> service_statusRef1);
>                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
> object[] { exception2.ToString() }), EventLogEntryType.Error);
>                  throw;
>            }
>            catch
>            {
>                  this.status.currentState = num1;
>                  NativeMethods.SetServiceStatus(this.statusHandle,
> service_statusRef1);
>                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
> object[] { string.Empty }), EventLogEntryType.Error);
>                  throw;
>            }
>      }
> }
>
>
>
>
>
Author
11 Dec 2006 9:19 PM
twahl
Thanks Gabriel,
I appreciate the response.  I took a look at the URL and I have tried what
is listed.  Services and the 'Service Control Manager' seem to be a different
animal.
Thanks again,
Tery

Show quote
"Gabriel Lozano-Morán" wrote:

> I know that the AppDomain.UnhandledException event will not be raised
> always. See here for a lead:
>
> http://msdn2.microsoft.com/en-us/library/system.windows.forms.unhandledexceptionmode.aspx
>
> Gabriel Lozano-Morán
> The .NET Aficionado
> http://www.pointerx.net
>
> "twahl" <tw***@discussions.microsoft.com> wrote in message
> news:C03FEF25-547D-4821-A464-646D5F8EF470@microsoft.com...
> > Hi,
> > I need to know if the AppDomain.UnhandledException event can be handled
> > using a Windows Service.  I've create a Windows Service that uses the
> > Exception Handling Application Block and there are certain exceptoions
> > that
> > I want bubbled up as unhandled exceptions.  I've used reflector to see
> > what
> > is going on in the Framework and it looks like the exception is re-thrown
> > so
> > I'm wondering what I am doing wrong?
> >
> > Thanks for your help in advance,
> > Terry
> >
> > private unsafe void DeferredStop()
> > {
> >      fixed (NativeMethods.SERVICE_STATUS* service_statusRef1 =
> > &this.status)
> >      {
> >            int num1 = this.status.currentState;
> >            this.status.checkPoint = 0;
> >            this.status.waitHint = 0;
> >            this.status.currentState = 3;
> >            NativeMethods.SetServiceStatus(this.statusHandle,
> > service_statusRef1);
> >            try
> >            {
> >                  this.OnStop();
> >                  this.WriteEventLogEntry(Res.GetString("StopSuccessful"));
> >                  this.status.currentState = 1;
> >                  NativeMethods.SetServiceStatus(this.statusHandle,
> > service_statusRef1);
> >                  if (this.isServiceHosted)
> >                  {
> >                        try
> >                        {
> >                              AppDomain.Unload(AppDomain.CurrentDomain);
> >                        }
> >                        catch (CannotUnloadAppDomainException exception1)
> >                        {
> >
> > this.WriteEventLogEntry(Res.GetString("FailedToUnloadAppDomain", new
> > object[]
> > { AppDomain.CurrentDomain.FriendlyName, exception1.Message }),
> > EventLogEntryType.Error);
> >                        }
> >                  }
> >            }
> >            catch (Exception exception2)
> >            {
> >                  this.status.currentState = num1;
> >                  NativeMethods.SetServiceStatus(this.statusHandle,
> > service_statusRef1);
> >                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
> > object[] { exception2.ToString() }), EventLogEntryType.Error);
> >                  throw;
> >            }
> >            catch
> >            {
> >                  this.status.currentState = num1;
> >                  NativeMethods.SetServiceStatus(this.statusHandle,
> > service_statusRef1);
> >                  this.WriteEventLogEntry(Res.GetString("StopFailed", new
> > object[] { string.Empty }), EventLogEntryType.Error);
> >                  throw;
> >            }
> >      }
> > }
> >
> >
> >
> >
> >
>
>
>

AddThis Social Bookmark Button