I have a topshelf windows service where I want to do some checking (i.e. if an xml file exists) and if the check fails I need the windows service to stop.
So I tried
I just ran into this issue and all the above answers seem to be over complicating things. All you need to do is use the WhenStarted
overload that accepts a Func
and return false if your internal service bootstrap failed. I don't think hostControl.Stop()
needs to be called explicitly.
//Here is bit from inside the .Service() call
s.WhenStarted((YourService svc, HostControl hc) => svc.Start());
//And the svc.Start method would look something like this:
class YourService
{
public bool Start() {
//return true if all is well
//or false if you want service startup to be halted
}
}