How to automatically start your service after inst

2019-01-13 14:55发布

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

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.

7条回答
The star\"
2楼-- · 2019-01-13 15:05

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 ServiceName property in the designer.

查看更多
萌系小妹纸
3楼-- · 2019-01-13 15:06

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.

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.)

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

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

查看更多
4楼-- · 2019-01-13 15:10

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

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.

查看更多
爷的心禁止访问
5楼-- · 2019-01-13 15:12

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.


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();
        }
    }
}
查看更多
聊天终结者
6楼-- · 2019-01-13 15:18

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.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-13 15:23

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.)

查看更多
登录 后发表回答