我使用以下代码安装Windows服务:
<field name="recipient"/>
从documentation我看到,此方法还会在服务上调用ServiceBase.Run(new ServiceProcess(serviceName, serviceArgs));
方法。但我希望将服务安装为已停止,然后手动启动它。
答案 0 :(得分:0)
private static InitialStartDone = false; // declared in service class
protected Overrride void OnStart(string[] args);
{
if(!InitialStartDone)
{
InitialStartDone = true;
}
else
{
base.OnStart(args);
}
}
尝试覆盖on start的默认行为,并使用静态变量来检测它是否是第一次调用
答案 1 :(得分:0)
我研究了我们的项目并找到了自定义安装程序。我重写方法OnCommitted
并检查参数Delayed
以立即开始服务或稍后手动完成。
[RunInstaller(true)]
public class CustomInstaller : System.Configuration.Install.Installer
{
public CustomInstaller()
{
_installProcess = new ServiceProcessInstaller { Account = ServiceAccount.NetworkService };
_installService = new CustomServiceInstaller(typeof(ServiceImplementation));
// Remove built-in EventLogInstaller:
_installService.Installers.Clear();
Installers.Add(_installProcess);
Installers.Add(_installService);
}
public override void Install(IDictionary stateSaver)
{
//install
base.Install(stateSaver);
}
protected override void OnCommitted(IDictionary savedState)
{
var delyed = bool.Parse(GetContextParameter(@"Delayed"));
if (!delyed)
{
new ServiceController(GetContextParameter(ServiceNameKey)).Start();
}
}
private string GetContextParameter(string parameterKey)
{
var parameterValue = Context.Parameters[parameterKey];
if (string.IsNullOrWhiteSpace(parameterValue))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, MissingRequiredParameterErrorMessage, parameterKey));
return parameterValue;
}
}