views:

3649

answers:

2

I'm writing a series of Windows services. I want them to fail if errors are thrown during startup (OnStart). I had assumed that merely throwing an error in OnStart would do this, but I'm finding that instead it "Starts" and presents me with a message stating "The service has started, but is inactive. Is this correct?" (Paraphrase). How do I handle the error so it actually fails to start the service?

+2  A: 

Move all of your startup logic to a separate method, and Throw exceptions (or call OnStop) from that seperate method.

OnStart has some oddities when starting up. I have found that if OnStart() has no more than one line in it, then I dont get the "The service started and then stopped.Some services stop automatically if they have no work to do" message, and thrown exceptions will terminate the process and log to the app event log.

Also with the seperate startup method, you can use a technique like this to debug it without attaching. http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx

StingyJack
+1  A: 

if you are running .NET 2.0 or higher, you can use ServiceBase.Stop.aspx) to stop the service from OnStart. Otherwise call Stop from a new thread.

ref devnewsgroups

Steven A. Lowe