在安装时自动启动Windows服务

mic*_*win 117 c# windows-services

我有一个Windows服务,我使用InstallUtil.exe安装.即使我已将启动方法设置为自动,但安装时服务无法启动,我必须手动打开服务并单击开始.有没有办法通过命令行或通过服务代码启动它?

cod*_*key 215

在Installer类中,为AfterInstall事件添加处理程序.然后,您可以在事件处理程序中调用ServiceController来启动该服务.

using System.ServiceProcess;

public ServiceInstaller()
{
    //... Installer code here
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
Run Code Online (Sandbox Code Playgroud)

现在,当您在安装程序上运行InstallUtil时,它将安装然后启动该服务.

  • (建议编辑的评论):最好使用serviceInstaller.ServiceName,如果servicename被更改,它将使用正确的名称,而无需在代码中更改它. (40认同)
  • @PhilipRego大概是`serviceInstaller`是事件处理程序中`sender`引用的`ServiceInstaller`对象,它通常在`ServiceInstaller()`构造函数中实例化.因此,您可以在`using`语句之前添加`ServiceInstaller serviceInstaller =(ServiceInstaller)sender;`. (4认同)
  • 你是如何获得serviceInstaller的? (3认同)

Ped*_*ira 27

重构一下后,这是一个完整的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 Start Type of Manual
        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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 此代码给出了以下错误:安装阶段发生异常.System.InvalidOperationException:System.ServiceProcess.ServiceInstaller的OnAfterInstall事件处理程序中发生异常.引发内部异常System.InvalidOperationException并显示以下错误消息:无法在计算机上启动服务serviceName'.'..内部异常System.ComponentModel.Win32Exception被抛出,并显示以下错误消息:此服务配置为的可执行程序run in不实现该服务. (2认同)
  • 一旦我注释掉"InitializeComponent()"行,就会查出错误.我相信这一行正在复制所有其他行,因为日志似乎在错误之前显示两个相同的事情:安装服务serviceName ...服务serviceName已成功安装.在日志应用程序中创建EventLog源serviceName ...安装服务serviceName ...在日志应用程序中创建EventLog源serviceName ... System.ServiceProcess.ServiceInstaller的OnAfterInstall事件处理程序中发生异常. (2认同)

Hem*_*ant 6

以下命令怎么样?

net start "<service name>"
net stop "<service name>"
Run Code Online (Sandbox Code Playgroud)


Ric*_*ard 5

控制服务的编程选项:

  • 可以使用本机代码,“启动服务”。最大程度的控制,最少的依赖,但最多的工作。
  • WMI:Win32_Service有一个StartService方法。这适用于您需要能够执行其他处理(例如选择哪个服务)的情况。
  • PowerShell:Start-Service通过RunspaceInvoke或通过创建自己RunspaceCreatePipeline方法并使用其方法执行来执行。这适用于您需要能够使用比 WMI 更简单的编码模型执行其他处理(例如选择哪个服务)的情况,但取决于安装的 PSH。
  • .NET 应用程序可以使用 ServiceController