How to automatically start your service after install?

.NetInstallationService

.Net Problem Overview


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.

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Solution 3 - .Net

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.

Solution 4 - .Net

thanks it run OK...

private System.ServiceProcess.ServiceInstaller serviceInstaller1;

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

Solution 5 - .Net

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.

Solution 6 - .Net

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();
        }
    }
}

Solution 7 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason ZView Question on Stackoverflow
Solution 1 - .NetJason ZView Answer on Stackoverflow
Solution 2 - .NetandynilView Answer on Stackoverflow
Solution 3 - .NetKeithView Answer on Stackoverflow
Solution 4 - .NetvisionnguyenView Answer on Stackoverflow
Solution 5 - .NetHeWillemView Answer on Stackoverflow
Solution 6 - .NetJeffrey RoughgardenView Answer on Stackoverflow
Solution 7 - .NetSagar KapadiaView Answer on Stackoverflow