Windows Service is crashing

  • Thread starter Thread starter Jason Barnett
  • Start date Start date
J

Jason Barnett

I'm not sure if this is the correct discussion group, so please advise me of
a better one if applicable.

I've created a windows service using v2.0 Framework. VS2005 created a
Program class, containing my Main method that instantiates and runs my
service. I've overridden the OnStart method to create a thread for
asynchronous processing. Within the thread's execution method, I call a
method on a referenced assembly to perform some work.

I've added try-catch blocks within every step to capure any Exception
object; within Main method, OnStart method, thread's execution method, and
referenced assembly's public method. However, My service displays an
"Unhandled Exception" popup, asking me if I want to debug. I don't
understand how any exception is getting by my error handling. Also, I don't
understand how to debug the process, since I can't attach it within VS2005
(it crashes too quickly), and the debug dialog doesn't bring me to any line
of code when I point it to an existing instance of my project.

Could someone please advise me what steps I can take to investigate and
resolve the issue?
 
Jason said:
I'm not sure if this is the correct discussion group, so please advise me of
a better one if applicable.

I've created a windows service using v2.0 Framework. VS2005 created a
Program class, containing my Main method that instantiates and runs my
service. I've overridden the OnStart method to create a thread for
asynchronous processing. Within the thread's execution method, I call a
method on a referenced assembly to perform some work.

I've added try-catch blocks within every step to capure any Exception
object; within Main method, OnStart method, thread's execution method, and
referenced assembly's public method. However, My service displays an
"Unhandled Exception" popup, asking me if I want to debug. I don't
understand how any exception is getting by my error handling. Also, I don't
understand how to debug the process, since I can't attach it within VS2005
(it crashes too quickly), and the debug dialog doesn't bring me to any line
of code when I point it to an existing instance of my project.

Could someone please advise me what steps I can take to investigate and
resolve the issue?

Add a handler to the AppDomain.UnhandledException event and have that log
the exception to somewhere useful. All managed exceptions pass through
there. If your problem is an exception in unmanaged code, you should see a
CLR error in the event log instead.

Also, for easier debugging of services, enable them to run as console
applications. This will not replicate the service environment (so if your
problem has to do with that it won't help) but it's very useful for
hammering out functional bugs without reinstalling every time. To do this,
change your .Main method to read something like this:

public static void Main(string args[]) {
if (Environment.UserInteractive) {
// not a service
MyService s = new MyService();
s.OnStart();
Console.ReadLine();
s.OnStop();
} else {
ServiceBase.Run(new MyService());
}
}

(Untested, but the general idea is sound.)
 
Thank you so much. By running it as a Console Application I was EASILY able
to identify the issue and correct it.

I also added the handler to the AppDomain.UnhandledException event to ensure
that nothing else will be missed (even if somethine strange happens in
production).

I appreciate your help.


Jeroen Mostert said:
Jason said:
I'm not sure if this is the correct discussion group, so please advise me of
a better one if applicable.

I've created a windows service using v2.0 Framework. VS2005 created a
Program class, containing my Main method that instantiates and runs my
service. I've overridden the OnStart method to create a thread for
asynchronous processing. Within the thread's execution method, I call a
method on a referenced assembly to perform some work.

I've added try-catch blocks within every step to capure any Exception
object; within Main method, OnStart method, thread's execution method, and
referenced assembly's public method. However, My service displays an
"Unhandled Exception" popup, asking me if I want to debug. I don't
understand how any exception is getting by my error handling. Also, I don't
understand how to debug the process, since I can't attach it within VS2005
(it crashes too quickly), and the debug dialog doesn't bring me to any line
of code when I point it to an existing instance of my project.

Could someone please advise me what steps I can take to investigate and
resolve the issue?

Add a handler to the AppDomain.UnhandledException event and have that log
the exception to somewhere useful. All managed exceptions pass through
there. If your problem is an exception in unmanaged code, you should see a
CLR error in the event log instead.

Also, for easier debugging of services, enable them to run as console
applications. This will not replicate the service environment (so if your
problem has to do with that it won't help) but it's very useful for
hammering out functional bugs without reinstalling every time. To do this,
change your .Main method to read something like this:

public static void Main(string args[]) {
if (Environment.UserInteractive) {
// not a service
MyService s = new MyService();
s.OnStart();
Console.ReadLine();
s.OnStop();
} else {
ServiceBase.Run(new MyService());
}
}

(Untested, but the general idea is sound.)
 
Back
Top