I have a standard windows server that inherits from the ServiceBase class.
On the OnStart method I want to check for certain conditions before I get to the main purpose of what my service does.
For example:
protected override void OnStart(string[] args) { if (condition == false) { EventLog.WriteEntry('Pre-condition not met, service was unable to start'); // TODO: Convert service state to 'Stopped' because my precondition wasn't met return; } InnitializeService(); }
Anybody have a good example for how a service can control its own state?
Throw an Exception. This will cause the services MMC to get an error – and the exception message and stack will automatically be logged to the event log. I use ApplicationException in this case.
In addition, the service will return to the ‘not running’ state.
If you need to stop later on, you can call the Stop method on your ServiceBase.