.net 安装后如何自动启动服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:21:04  来源:igfitidea点击:

How to automatically start your service after install?

.netinstallerservice

提问by Jason Z

How do you automatically start a service after running an install from a Visual Studio Setup Project?

从 Visual Studio 安装项目运行安装后,如何自动启动服务?

I just figured this one out and thought I would share the answer for the general good. Answer to follow. I am open to other and better ways of doing this.

我刚刚想通了这一点,并认为我会为了普遍利益而分享答案。回答跟随。我对其他更好的方法持开放态度。

回答by Jason Z

Add the following class to your project.

将以下类添加到您的项目中。

using System.ServiceProcess;  

class ServInstaller : ServiceInstaller
{
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        ServiceController sc = new ServiceController("YourServiceNameGoesHere");
        sc.Start();
    }
}

The Setup Project will pick up the class and run your service after the installer finishes.

安装项目将在安装程序完成后选择该类并运行您的服务。

回答by andnil

Small addition to accepted answer:

接受答案的小补充:

You can also fetch the service name like this - avoiding any problems if service name is changed in the future:

您还可以像这样获取服务名称 - 避免将来更改服务名称时出现任何问题:

protected override void OnCommitted(System.Collections.IDictionary savedState)
{
    new ServiceController(serviceInstaller1.ServiceName).Start();
}

(Every Installer has a ServiceProcessInstaller and a ServiceInstaller. Here the ServiceInstaller is called serviceInstaller1.)

(每个安装程序都有一个 ServiceProcessInstaller 和一个 ServiceInstaller。这里 ServiceInstaller 被称为 serviceInstaller1。)

回答by Keith

This approach uses the Installer class and the least amount of code.

这种方法使用安装程序类和最少的代码。

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace MyProject
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
        }
    }
}

Define serviceInstaller1(type ServiceInstaller) in the Installer class designer and also set its ServiceNameproperty in the designer.

serviceInstaller1在安装程序类设计器中定义(键入 ServiceInstaller)并在设计器中设置其ServiceName属性。

回答by Keith

thanks it run OK...

谢谢它运行正常...

private System.ServiceProcess.ServiceInstaller serviceInstaller1;

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController("YourServiceName");
    sc.Start();
}

回答by HeWillem

Instead of creating your own class, select the service installer in the project installer and add an event handler to the Comitted event:

不是创建自己的类,而是在项目安装程序中选择服务安装程序,并向 Comitted 事件添加一个事件处理程序:

private void serviceInstallerService1_Committed(object sender, InstallEventArgs e)
{
    var serviceInstaller = sender as ServiceInstaller;
    // Start the service after it is installed.
    if (serviceInstaller != null && serviceInstaller.StartType == ServiceStartMode.Automatic)
    {
        var serviceController = new ServiceController(serviceInstaller.ServiceName);
        serviceController.Start();
    }
}

It will start your service only if startup type is set to automatic.

仅当启动类型设置为自动时,它才会启动您的服务。

回答by Jeffrey Roughgarden

Based on the snippets above, my ProjectInstaller.cs file wound up looking like this for a service named FSWServiceMgr.exe. The service did start after installation. As a side note, remember to click on the Properties tab (not right-click) when the setup project is selected in the Solution Explorer to set the company and so forth.

根据上面的片段,对于名为 FSWServiceMgr.exe 的服务,我的 ProjectInstaller.cs 文件看起来像这样。该服务确实在安装后启动。作为旁注,当在解决方案资源管理器中选择安装项目以设置公司等时,请记住单击“属性”选项卡(而不是右键单击)。



using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace FSWManager {
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer {
        public ProjectInstaller() {
            InitializeComponent();
            this.FSWServiceMgr.AfterInstall += FSWServiceMgr_AfterInstall;
        }

        static void FSWServiceMgr_AfterInstall(object sender, InstallEventArgs e) {
            new ServiceController("FSWServiceMgr").Start();
        }
    }
}

回答by Sagar Kapadia

There is also another way which does not involve code. You can use the Service Control Table. Edit the generated msi file with orca.exe, and add an entry to the ServiceControl Table.

还有另一种不涉及代码的方式。您可以使用服务控制表。使用 orca.exe 编辑生成的 msi 文件,并将条目添加到ServiceControl Table

Only the ServiceControl, Name,Event and Component_ columns are mandatory. The Component_ column contains the ComponentId from the File Table. (Select the File in the file table, and copy the Component_value to the ServiceControl table.)

只有 ServiceControl、Name、Event 和 Component_ 列是必需的。Component_ 列包含文件表中的 ComponentId。(选择文件表中的文件,将 Component_value 复制到 ServiceControl 表中。)

The last step is to update the value of StartServices to 6575 in table InstallExecutesequence. This is sufficient to start the service.

最后一步是将表 InstallExecutesequence 中的 StartServices 的值更新为 6575。这足以启动服务。

By the way, the service install table allows you to configure the installer to install the windows service.

顺便说一下,服务安装表允许您配置安装程序以安装 Windows 服务。