Windows Service - stoping the OnStart event in the OnStart event

N

Next

Hello,

In my OnStart event I have several checks to make sure values in the config
file are correct.

If any of the values are incorrect I don't want the service to start.

Is there a "cancel event" of some sort I can use in the OnStart event?

Thanks in advance for your help,
Aaron
 
G

Glen Richards

I'm in the same boat.

I've tried this:

ServiceController sc = new ServiceController(this.ServiceName);
sc.Stop();
sc.Dispose();

& it works, but only very tempermentally - & I don't trust it.

I've seen posts saying that the official documentation says not to use
the code above - so what is the official way of handling errors on
service start up.

Other ideas I've seen are triggering a seperate process/thread that
calls "net stop servicename", but this seems to be a nasty hack -
surely there is an "official way" to handle this issue.

Cheers,


Glen
 
J

Jay B. Harlow [MVP - Outlook]

Next & Glen,
If your override of ServiceBase.OnStart throws an exception, the service
will not be started.

If ServiceBase.AutoLog is True, a message indicating the service did not
start will be written to the event log.

Hope this helps
Jay
 
G

Glen Richards

Thanks for the advice Jaj.

I was catching my exception, so I tried "not handling" the exception &
it didn't resolve my problem (ie the service did start).

I also tried rethrowing the exception "thow (e)" - and this didn't
help.

I've done a search in the msdn library for some documentation regarding
your suggestion - but couldn't find any mention.

What am I missing? Any references to the documentation/web/code snips
would be appeciated.

Cheers for your help.


Glen.
 
G

Glen Richards

Edit|Undo

Sorry Jay, your advice seems to work.

Re-throwing my exception & cutting out my manual "stopping" code works.

Cheers,


Glen
 
J

Jay B. Harlow [MVP - Outlook]

Glen,
I've done a search in the msdn library for some documentation regarding
your suggestion - but couldn't find any mention.
I have not seen it documented anyplace other then numerous newsgroup posts,
by myself & others.

Hope this helps
Jay
 

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