Click here to Skip to main content
15,891,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking to build a Windows service for the first time in ages, and have lost touch a bit. I want to configure the service so it basically always restarts on failure. It's quite innocuous, and umpteen web service calls mean many points of failure.

How do I e.g. in the event of an unhandled exception, or a handled one from which there is no turning back, signal to Windows that my service has failed?
Posted
Updated 13-Nov-14 2:20am
v3
Comments
ZurdoDev 13-Nov-14 8:02am    
Are you doing a web service or a windows service?
Brady Kelly 13-Nov-14 8:05am    
Um, a Windows Service.
ZurdoDev 13-Nov-14 8:16am    
You may want to update the question then because you mention building a web service.

However, a windows service only fails when an exception is thrown that you never catch that causes the service to fail. In the Services manager you can choose what to do when it fails. Just open Administrative Tools from Control Panel, then Services, and you configure the service there.
Brady Kelly 13-Nov-14 8:19am    
Oops, thanks, fixed that. I know how to configure what happens when it fails, just wasn't sure of what the failure entails. If it's just an unhandled exception I can simply remove my outermost handler and let Windows 'catch' the exception.
ZurdoDev 13-Nov-14 8:23am    
The goal, of course, is to never have it fail. Even though there is no UI think of it as a regular exe and if it crashes then that is when the restart options come into play.

 
Share this answer
 
Comments
Brady Kelly 13-Nov-14 8:45am    
Thanks Kornfeld, interesting article, but simply knowing how to manually configure Recovery and Autostart is enough for this iteration. My question is rather motivated by wanting to know how Windows detects a failure, not how to react to the failure.
Kornfeld Eliyahu Peter 13-Nov-14 9:01am    
Services in Windows run under Service Control Manager. This manager has a database with all the properties - including recovery options. A running service is the child process of the manager, so it informed when it fails and manager can decide - according to properties - what to do...By default .NET has method to install services, but it has limited functionality. The article explains how to invoke the method that has more options...
Brady Kelly 13-Nov-14 9:10am    
I understand all that. My question was all about how do I exit my service process, upon a failure condition, in order to properly signal the service manager that my process has failed. If I don't have to do anything, and the manager just keeps an eye on al running services, that is sweet.
Kornfeld Eliyahu Peter 13-Nov-14 9:14am    
You only have to ensure that you exit with exit code different from 0...
Brady Kelly 13-Nov-14 9:24am    
Thanks. Googling the 'exit code' question led me to this StackOverflow answer, which is what I was actually looking for. http://stackoverflow.com/a/4197271/8741
As discussed in the comments, you certainly don't want your service to fail but it will fail just like a regular exe will: when you don't catch an exception or if the system really blows up somehow. The restart options are configured in the Services properties.
 
Share this answer
 
Comments
Brady Kelly 13-Nov-14 9:26am    
Thank you, but unfortunately - in a CP light - I found my answer on StackOverflow, as I mention above in a comment to Kornfeld.
ZurdoDev 13-Nov-14 9:27am    
Good to hear.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900