[ACCEPTED]-How to catch exception and stop Topshelf service?-topshelf
You could use the HostControl object and 3 modify your method like this:
public bool Start(HostControl hostControl)
{
if (!File.Exists(_xmlFile)
{
hostControl.Stop();
return true;
}
// Do some work here if xml file exists.
...
}
And you will 2 need to pass the HostControl in to the Start 1 method like this:
HostFactory.Run(conf =>
{
conf.Service<YourService>(svcConf =>
{
svcConf.WhenStarted((service, hostControl) =>
{
return service.Start(hostControl);
}
}
}
Each of the WhenXxx methods can also take 8 an argument of the HostControl interface, which 7 can be used to request the service be stopped, request 6 additional start/stop time, etc.
In such 5 case, change signature of start() to be 4 bool start(HostControl hc). Retain reference 3 to this HostControl in the service as follow:
public bool Start(HostControl hc)
{
hostControl = hc;
Restart();
return true;
}
Now 2 when you want to stop the service use following 1 call:
hostControl.Stop();
I was curious about this from the point 27 of view of a best practice or recommendation 26 in Topshelf's documentation, but couldn't 25 find anything. I did, however, find two 24 separate comments from phatboyg...
Best comment... how 23 to stop service on exception, via this issue (I cut 22 out some of the detail):
If your service's 21 Start method throws an exception, the service 20 will fail to start.
Once the service is running, if 19 an unhandled exception is thrown, the service 18 will stop, and report it as a crash to the 17 service control manager.
If you need to 16 Stop your service programatically, use the 15 HostControl method Stop.
So I think the 14 easiest answer is to throw an exception.
You 13 were doing that, and you mention "the 12 windows service stays around as a process 11 after the exception". That seems like 10 an unrelated bug somewhere in your code, or 9 perhaps you somehow had multple instances 8 running? I've been testing these scenarios 7 this morning and have not seen my service 6 running after throwing an Exception in the 5 start method.
Also, relevant to checking 4 before HostFactory.Run, mentioned in the 3 accepted answer, via https://groups.google.com/forum/embed/#!topic/topshelf-discuss/nX97k3yOhJU:
"Your application 2 should do nothing more than configure NLog/Log4Net 1 before calling the HostFactory.Run() method."
I just ran into this issue and all the above 5 answers seem to be over complicating things. All 4 you need to do is use the WhenStarted
overload that 3 accepts a Func<T,HostControl,bool>
and return false if your internal 2 service bootstrap failed. I don't think 1 hostControl.Stop()
needs to be called explicitly.
//Here is bit from inside the .Service<T>() 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
}
}
I've "borrowed" the sample code for the 12 functional setup of topshelf to demonstrate 11 a point:
HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name=> new TownCrier()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); //6
x.SetDescription("Sample Topshelf Host"); //7
x.SetDisplayName("Stuff"); //8
x.SetServiceName("stuff"); //9
});
You're going to have to place your 10 file system check BEFORE the above code 9 runs. Let's think about this a second. The 8 point of having a service is to make sure 7 it RUNS and KEEPS RUNNING. You're attempting 6 to subvert a basic principle of having service 5 applications in the first place. Instead 4 of trying to stop the service because of 3 the missing file, figure out some way to 2 alert your support staff and NOT do whatever 1 depends on that missing file.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.