2

I need my service to check for existence and structure of certain files during its startup and exit/fail/stop if some conditions aren't met. I read this thread: What is the proper way for a Windows service to fail? but it does not help.

I set the ServiceBase.ExitCode property non-zero and then call ServiceBase.Stop. But I get 5 event log entries. See below:

Starting service. (I log this event via code)
Config.xml file not found. (I log this ERROR event via code)
Service stopped successfully. (SCM logs this message)
Service started successfully. (SCM logs this message)
Service cannot be started. The handle is invalid  (SCM logs this message)

As you see everything goes OK except for the last two entries. Why are they there? What can I do to properly shutdown the service during startup? Why doesn't SCM see the service as stopped/failed?

Community
  • 1
  • 1
Daniel
  • 1,391
  • 2
  • 19
  • 40
  • I found throwing an exception instead of calling Stop() prevents the OnStop event from being invoked, and I suspect won't log the "stopped successfully" messages you're seeing. The only thing I can find so far that seems to be a list of possible exit codes is http://www.hiteksoftware.com/mize/Knowledge/articles/049.htm. If I use an exit code not in the list, I get a message like ["Error 10501: 0x2905"](http://i.imgur.com/UohWLVr.png) (x2905 = hex value of 10501). Is there no MSDN documentation on the exit codes, or guidance on how to signal to a user how to debug the issue? – gregmac Jul 19 '13 at 19:36

3 Answers3

1

You don't provide enough code to really know, but I suspect you are trying to validate the service and stop it in either the constructor or the OnStart. The way I like to handle services is start my timer in the OnStart. In the first interval of the timer I can validate all the code, if its invalid close the Service. If its valid, reset the interval of the timer to how frequently I want it to run then set a bool that tells it not to check for validity of files again.

edepperson
  • 1,035
  • 1
  • 14
  • 33
0

What is the return code you are using for your ExitCode? If it matches the corresponding windows ExitCode, then that is what will be recorded by SCM. I'm assuming you are returning a 6 for your ExitCode.

The other thing is if you can run on Default values do that, let Config.xml be missing and just record the problem in the EventLog. "Configuration file Missing"

If you really want it to just abort during OnStart, set your ExitCode and then Throw an Exception (InvalidArgumentException, InvalidOperationException) for example

This article also has some good advice. .NET: Which Exception to Throw When a Required Configuration Setting is Missing?

Community
  • 1
  • 1
Paul Farry
  • 4,730
  • 2
  • 35
  • 61
0

You are trying to start second instance of service (another service registered for the same .exe)

Vadim
  • 1