Abort service start in OnStart

M

Mike Frank

What is the correct way to abort a service start in the OnStart method
if things were detected which will not let the service run properly?

I could simply throw an exception and the serice will not be started,
but this does not seem right. Additionally a very confusing message is
output in this case by the system.

Mike
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

Actually, I would throw an exception if there was a truly unexpected
error (and log it in the event log as well). Either that, or I would call
Stop on myself (and set the ExitCode as well, if you wanted).

I would think that Stop would handle the shutdown gracefully.

Hope this helps.
 
O

Ollie Riches

As Nick says throwing an exception is acceptable, be aware that any
exception thrown during the start up\shutdown of a service is automatically
logged to the system event log (application log).

Any message output during the start up\shutdown of service is indictative of
a some kind of failure or undesired side affect, and yes they aren't in
general very descriptive or helpful to someone who is not experienced with
using windows services frequently.

HTH

Ollie Riches
 
M

Mike Frank

Thanks to both of you.

I'll go for the exception then. When using ServiceBase.Stop the messages
in the eventlog are even mor confusing.

Mike
 

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