C# 如何使Windows服务作为“启动”;自动(延迟启动)“;
场景: 作为Windows服务运行的WCF服务。帐户是“用户”C# 如何使Windows服务作为“启动”;自动(延迟启动)“;,c#,windows-services,wcf,C#,Windows Services,Wcf,场景: 作为Windows服务运行的WCF服务。帐户是“用户” 做了什么: 我已经在projectinstaller中重写了OnBeforeInstall,以便能够从配置文件中设置用户名和密码 我能做什么: 我希望能够将starttype设置为自动(延迟启动) 我尝试过的: 我将以下代码行放在覆盖的onbeforeeinstall中 serviceInstaller1.StartType=ServiceStartMode.Automatic+1 我想我会欺骗ServiceStartMode
做了什么: 我已经在projectinstaller中重写了OnBeforeInstall,以便能够从配置文件中设置用户名和密码
我能做什么: 我希望能够将starttype设置为自动(延迟启动)
我尝试过的: 我将以下代码行放在覆盖的onbeforeeinstall中
serviceInstaller1.StartType=ServiceStartMode.Automatic+1代码>
我想我会欺骗ServiceStartMode枚举来表示自动(延迟启动),但没有成功。我没有尝试过更多的东西,因为我找不到任何可以尝试的东西
我在网上找到的信息:
我发现自动(延迟启动)将在.NET4中可用,但这对我现在没有帮助。
我发现DelayedAutoStart可以添加到服务的配置密钥中,但如果我从代码中这样做的话,这感觉像是一种黑客行为。但也许这是目前我唯一的解决方案
有什么想法吗
Robert Persson,瑞典您唯一的其他选择是使用p/invoke调用服务配置延迟自动启动信息
。但是,由于您似乎不愿意添加注册表项,我怀疑您是否希望使用P/invoke。在.NET Framework(<4.0)中没有其他方法可以实现这一点。既然.NET 4.0已经出现了:
serviceInstaller1.StartType = ServiceStartMode.Automatic;
serviceInstaller1.DelayedAutoStart = true;
对于.NET Framework 3.5项目,我可以通过手动设置我的服务的DelayedAutostart
值,将我的服务安装为“自动(延迟)”服务。例如:
public ProjectInstaller()
{
...
AfterInstall += ProjectInstaller_AfterInstall;
}
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
string serviceName = <YourSpecific>Installer.ServiceName;
using (RegistryKey serviceKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Services\" + serviceName, true))
{
serviceKey.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord);
}
}
publicprojectinstaller()
{
...
AfterInstall+=项目安装程序\u AfterInstall;
}
安装后无效ProjectInstaller_(对象发送器,InstallEventArgs e)
{
字符串serviceName=Installer.serviceName;
使用(RegistryKey serviceKey=Registry.LocalMachine.OpenSubKey(@“System\CurrentControlSet\Services\”+serviceName,true))
{
SetValue(“DelayedAutostart”,1,RegistryValueKind.DWord);
}
}
请注意,安装该服务后,该服务将不会被列为“自动(延迟)”,直到计算机重新启动后我将对jdnight进行一点扩展。我在尝试他的解决方案时遇到了写权限问题,所以我做了如下操作:
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("System", true); //Opens the System hive with writting permissions set to true
key = key.CreateSubKey("CurrentControlSet"); //CreateSubKey opens if subkey exists, otherwise it will create that subkey
key = key.CreateSubKey("services");
key = key.CreateSubKey(serviceInstaller1.ServiceName);
key.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
我还通过添加InstallEventHandler的新实例注册了AfterInstall事件。我不确定这是否真的有必要,但也不会有什么坏处:
AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
在.NETFramework2.0上工作起来很有魅力。正如前面所指出的,对于框架4和更高版本,使用
serviceInstaller1.DelayedAutoStart = true;
根据菲亚特的回答。谢谢!如果可能的话,承包商希望服务自动(延迟启动),但这并不重要。所以我们要等到.NET4。谢谢你的回答。有人知道这段代码是在哪里添加的吗?ServiceInstaller.cs?在ServiceInstaller()的构造函数中?在服务的构造函数中?我在ProjectInstaller.Designer.cs文件中有它以及凭据等。感谢您提供有关重新启动的提示。当我看到注册表项在那里时,我正在寻找它不起作用的原因。您好,jdknight,是否必须更改“自动(延迟启动)”的注册表设置?对于.NET 3.5,是这样。如果您使用的是新框架,请参阅。