I'm following along with http://msdn.microsoft.com/en-us/library/bb332338.aspx to host my service as a Windows service. It installs just fine, but I would like to actually set the recovery options to "Restart the Service" on a "First failure", "Second failure", or "Subsequent failure". How can this be achieved?
Asked
Active
Viewed 1,581 times
2 Answers
4
The options for doing so are not simple, they require invoking some methods to pull it off. You could use something like the following extension, http://www.codeproject.com/Articles/6164/A-ServiceInstaller-Extension-That-Enables-Recovery or rolling your own using the commands.
[DllImport("advapi32.dll", EntryPoint="ChangeServiceConfig2")]
public static extern bool
ChangeServiceFailureActions( IntPtr hService, int dwInfoLevel,
[ MarshalAs( UnmanagedType.Struct ) ] ref SERVICE_FAILURE_ACTIONS lpInfo );
[DllImport("advapi32.dll", EntryPoint="ChangeServiceConfig2")]
public static extern bool
ChangeServiceDescription( IntPtr hService, int dwInfoLevel,
[ MarshalAs( UnmanagedType.Struct ) ] ref SERVICE_DESCRIPTION lpInfo );
See http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7660 for more information

Charlie Brown
- 2,817
- 2
- 20
- 31
-
Ah, this seems like the more proper way to do it. – Alexandru Aug 23 '13 at 13:49
2
You can set recovery options using sc.exe from the command line. This answer has a good example of how to do this using C#:

Community
- 1
- 1

chris.house.00
- 3,273
- 1
- 27
- 36
-
-
-
For the record, I went with doing it this way. The above method is the better way to invoke it, but intent is not as clear to someone reading your code. I think this is the clearest way to achieve this. – Alexandru Aug 23 '13 at 15:52