连同我托管的Windows服务';的安装程序,是否可以在出现错误时设置恢复选项

本文关键字:是否 错误 选项 恢复 设置 程序 Windows 服务 安装 | 更新日期: 2023-09-27 18:28:50

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

连同我托管的Windows服务';的安装程序,是否可以在出现错误时设置恢复选项

这样做的选项并不简单,它们需要调用一些方法才能实现,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获取更多信息

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

安装Windows服务并执行恢复操作以重新启动