.net 使用恢复操作安装 Windows 服务以重新启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1633429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:29:46  来源:igfitidea点击:

Install Windows Service with Recovery action to Restart

.netwindows-servicesservice

提问by Ray

I'm installing a Windows Service using the ServiceProcessInstallerand ServiceInstallerclasses.

我正在使用ServiceProcessInstallerServiceInstaller类安装 Windows 服务。

I've used the ServiceProcessInstallerto set the start type, name, etc. But how do I set the recovery action to Restart?

我已经使用ServiceProcessInstaller来设置启动类型、名称等。但是如何将恢复操作设置为重新启动?

I know I can do it manually after the service is installed by going to the Services management console and changing the settings on the recovery tab of the service's properties, but is there a way to do it during the install?

我知道我可以在安装服务后通过转到服务管理控制台并更改服务属性的恢复选项卡上的设置来手动执行此操作,但是有没有办法在安装过程中执行此操作?

Service Property Recovery Tab

服务属性恢复选项卡

回答by Kevin

You can set the recovery options using sc. The following will set the service to restart after a failure:

您可以使用sc设置恢复选项。以下将设置服务在失败后重新启动:

sc failure [servicename] reset= 0 actions= restart/60000

This can easily be called from C#:

这可以很容易地从 C# 调用:

static void SetRecoveryOptions(string serviceName)
{
    int exitCode;
    using (var process = new Process())
    {
        var startInfo = process.StartInfo;
        startInfo.FileName = "sc";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;

        // tell Windows that the service should restart if it fails
        startInfo.Arguments = string.Format("failure \"{0}\" reset= 0 actions= restart/60000", serviceName);

        process.Start();
        process.WaitForExit();

        exitCode = process.ExitCode;
    }

    if (exitCode != 0)
        throw new InvalidOperationException();
}

回答by Juan Carlos Velez

After many attemps, I resolved it using sccommand line app.

经过多次尝试,我使用sc命令行应用程序解决了它。

I have batch file with installutil and sc. My batch file is similar to:

我有带有 installutil 和 sc 的批处理文件。我的批处理文件类似于:

installutil.exe "path to your service.exe"
sc failure "your service name" reset= 300 command= "some exe file to execute" actions= restart/20000/run/1000/reboot/1000

If you want the full documentation of sc command, follow this link: SC.exe: Communicates with the Service Controller and installed services

如果您需要 sc 命令的完整文档,请点击此链接:SC.exe:与服务控制器和已安装的服务通信

Note: You need to add an space after each equal (=) symbol. Example: reset= 300

注意:您需要在每个等号 (=) 后添加一个空格。示例:重置= 300

回答by Ron Klein

I found the following project which takes care of these settings, using only code and Win API calls:
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac

我发现以下项目负责这些设置,仅使用代码和 Win API 调用:http:
//code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac