我有一个Windows服务,我使用InstallUtil.exe进行安装。尽管我将启动方式设置为自动,但服务在安装后不会自动启动,我必须手动打开服务并单击启动。是否有通过命令行或服务代码启动它的方法?
我有一个Windows服务,我使用InstallUtil.exe进行安装。尽管我将启动方式设置为自动,但服务在安装后不会自动启动,我必须手动打开服务并单击启动。是否有通过命令行或服务代码启动它的方法?
在您的Installer类中,添加一个处理程序用于AfterInstall事件。然后可以在事件处理程序中调用ServiceController来启动服务。
using System.ServiceProcess;
public ServiceInstaller()
{
//... Installer code here
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceInstaller serviceInstaller = (ServiceInstaller)sender;
using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
{
sc.Start();
}
}
现在当你在安装程序上运行InstallUtil时,它将自动安装并启动服务。
经过一些重构后,这是一个完整的Windows服务安装程序示例,具有自动启动功能:
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace Example.of.name.space
{
[RunInstaller(true)]
public partial class ServiceInstaller : Installer
{
private readonly ServiceProcessInstaller processInstaller;
private readonly System.ServiceProcess.ServiceInstaller serviceInstaller;
public ServiceInstaller()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new System.ServiceProcess.ServiceInstaller();
// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Automatic Start Type
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Windows Automatic Start Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
serviceInstaller.AfterInstall += ServiceInstaller_AfterInstall;
}
private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("Windows Automatic Start Service");
sc.Start();
}
}
}
如下是相关命令:
net start "<service name>"
net stop "<service name>"
控制服务的编程选项:
StartService
方法。这适用于需要执行其他处理(例如选择哪个服务)的情况。RunspaceInvoke
执行 Start-Service
或创建自己的 Runspace
并使用其CreatePipeline
方法执行。这适用于需要执行其他处理(例如选择哪个服务)且编码模型比 WMI 容易得多的情况,但需要安装 PSH。ServiceController
net start *servicename*
ProjectInstaller
过程和代码示例:
ProjectInstaller
(安装程序创建时应该会自动打开),设置生成的serviceProcessInstaller1
(例如账户:LocalSystem)和serviceInstaller1
(例如启动类型:自动)的属性。ProjectInstaller
(在设计编辑器中按下F7
),并添加ServiceInstaller.AfterInstall
事件处理程序,参见以下代码。它将在安装后启动服务。ProjectInstaller类:
using System.ServiceProcess;
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent(); //generated code including property settings from previous steps
this.serviceInstaller1.AfterInstall += Autorun_AfterServiceInstall; //use your ServiceInstaller name if changed from serviceInstaller1
}
void Autorun_AfterServiceInstall(object sender, InstallEventArgs e)
{
ServiceInstaller serviceInstaller = (ServiceInstaller)sender;
using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
{
sc.Start();
}
}
}
使用ServiceController从代码中启动您的服务。
更新:从命令行启动服务的更正确的方法是使用“sc”(服务控制器)命令,而不是“net”。
尽管我完全按照所接受的答案操作,但我仍然无法启动服务——在安装过程中,我收到了一个失败消息,指出刚刚安装的服务无法启动,因为它不存在,尽管使用了this.serviceInstaller.ServiceName
而不是文字...
最终,我找到了一种替代方案,利用命令行:
private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C sc start " + this.serviceInstaller.ServiceName;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
}
你的设计师出现了问题。重新添加你的安装程序组件。它应该有一个serviceInstaller和一个serviceProcessInstaller。将Startup Method属性设置为Automatic的serviceInstaller将在安装后和每次重启后启动。
注意:您可能已经使用表单界面设置了不同的服务安装程序和项目安装程序。在这种情况下,将serviceInstaller.ServiceName替换为“设计师名称”。ServiceName。
在这种情况下,您也不需要私有成员。
感谢您的帮助。
ServiceInstaller
变量。这个类应该实现System.Configuration.Install.Installer
接口。更多信息请参见此 msdn 指南。 - Sergio BasurcoserviceInstaller
是事件处理程序中sender
所引用的ServiceInstaller
对象,通常在ServiceInstaller()
构造函数中实例化。因此,在using
语句之前添加ServiceInstaller serviceInstaller = (ServiceInstaller)sender;
。 - khargoosh