1

I am using this method to create a windows service.

public void InstallService(string username, string password, string configurationUser, string configurationPassword)
{
    var installationPath = GetInstallationPath();
    var process = installationPath + @"myservice.exe ";
    args = "-install " + username + " " + password;
    InstallProcessWithLogOn(process, args, username, password, configurationUser, configurationPassword, 90 * 1000);
    // InstallProcessWithLogOn uses the method CreateProcessWithLogonW
}

As default, my service, in the Recovery tab, has "Take no action" at First failure, Second failure and Third failure.

How can I modify my method such that when the service is created it has the option set as "Restart the Service" for First failure, Second failure and Third failure?

LE : Is there any way of configuring that service using advapi32.dll's method CreateProcessWithLogonW? Or something else from advapi32.dll?

dizzyOWNZ
  • 11
  • 1
  • 4
  • Have you tried the `ServiceInstaller` provided by .Net? – Sebastian L Mar 06 '17 at 08:00
  • We'd need to have *some clue* about what `myservice.exe` *actually does* when run with the `-install` argument to have a chance of being able to answer this. – Damien_The_Unbeliever Mar 06 '17 at 08:05
  • How does knowing what myservice.exe help you? I just want to configure the service such that it has those options set in the Recovery Tab when installing it. – dizzyOWNZ Mar 06 '17 at 09:13
  • @dizzyOWNZ - because you're not showing us the code that *installs the service*, and it's *that* code that is likely to need updating/changing/replacing. – Damien_The_Unbeliever Mar 06 '17 at 10:25
  • @Damien_The_Unbeliever I think you are right, I'm going to investigate that file and come back to an answer but so far the solution below will work for now. – dizzyOWNZ Mar 06 '17 at 12:05
  • @Damien_The_Unbeliever Is there any way of modifying the serivice using advapi32.dll? – dizzyOWNZ Mar 07 '17 at 06:27

1 Answers1

3

I ran into this issue with a Service that I had a while back. I am not sure if you can do this directly in code, but you can set it manually at a command prompt by calling sc failure. The option that I ended up using was to execute the command after the Installation had been committed, by attaching to the Install.Commited event handler

var serviceName = "YourWindowsServiceName";
var resetAfter = 60000;
Process.Start("cmd.exe", $"/c sc failure \"{serviceName}\" reset= 0 actions= restart/{resetAfter}/restart/{resetAfter}/restart/{resetAfter}");

The actions parameter above takes a slash delimited set of configurations. They alternate between Action and Time (in ms). So the Above looks like:

  • FirstFailure: restart @ 60seconds
  • Second Failure: restart @ 60seconds
  • Third Failure: restart @ 60seconds

See more about sc failure.

pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63