Windows Service - ExceptionHandling after error in OnStart()-method

W

wundertier

Hi,

I have written a windows service in C#. This service may encounter an
error during startup, i.e. if the user running the service does not
have the rights required for other actions. This error happens due to
wrong service configuration. As the configuration may change during
normal productive usage, I want to catch this error, log it of course
and show an appropriate message to the user.

There are two ways to start the service:
1. Using windows control panel, administrative tools, services: after
trying to start the service, I get a dialog window telling me:
"The XYService service on Local Computer started and then stopped.
Some services stop automatically if they have no work to do, for
example, the Performance Logs and Alerts service."

This is not really useful and in my case even wrong, but at least the
service is stopped gracefully (as far as I can see) and nothing
"hangs".

2. I have written a C# application which is used to ease the
configuration of this service. It also offers a possibility to start
the service. If I try to start the service from this application, the
app "hangs" and never returns.

The OnStart() method of the service looks like this:

protected override void OnStart(string[] args)
{
try
{
// here the service is initialized
// and somewhere in here the error happens
...
}
catch (Exception ex)
{
// the following message gets logged regardless of which start
method I use
logger.Error("OnStart threw an Exception.", ex);
throw;
}
}


The program flow is as I think it should be, i.e. the error happens in
the try-block and the exception gets logged in the catch-block.
As I don't want the service to stay alive in case of an error, I just
rethrow the exception.

What do I have to do/change here
- to avoid the hangup of the application when there was an error
during startup? (when using start method 2)
- to display a more useful error message? (when using start method 1)

Thank you very much for your help!

Steffi

PS: I already use an UnhandledExceptionEventHandler. But it doesn't
execute in this case (at least I don't find any of the event log
messages it normally writes).
 
W

wundertier

AFAIK, you don't have control over the error message when starting via
the Services control panel.
:-( ... ok, the user has to live with it then.
 Your service should use the event log to
log the error, where the user then can be expected to look for the details.
.... depends on the user :) ... but yes, you are right.
 I doubt the exception
that occurs in the service, which is running in an entirely different
process, most likely under a different user even, would be propagated
back to the application.

Well, this sentence made me look at it again from the point of view of
the application ... and ... hmm
I've always looked *into* the service and its OnStart method only, but
never into the app at the point where I trigger the call of the
service method ...
There I've found something like this:

ServiceController svCon;
....
svCon.Start();
....
svCon.WaitForStatus(ServiceControllerStatus.Running);
....

*ouch!*
I am quite embarrassed. :-\

Thank you for your help! You made me find this.

Steffi
 

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