3

我与http://msdn.microsoft.com/en-us/library/bb332338.aspx一起将我的服务托管为 Windows 服务。它安装得很好,但我想在“第一次失败”、“第二次失败”或“后续失败”时将恢复选项设置为“重新启动服务”。如何做到这一点?

4

2 回答 2

4

这样做的选项并不简单,它们需要调用一些方法来实现。您可以使用类似以下扩展的内容,http://www.codeproject.com/Articles/6164/A-ServiceInstaller-Extension-That-Enables-Recovery或使用命令自行滚动。

[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 );

有关详细信息,请参阅http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7660

于 2013-08-23T13:49:19.053 回答
2

您可以从命令行使用 sc.exe 设置恢复选项。这个答案有一个很好的例子来说明如何使用 C# 做到这一点:

安装带有恢复操作的 Windows 服务以重新启动

于 2013-08-23T13:43:38.907 回答