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.
This approach uses the Installer class and the least amount of code.
Define
serviceInstaller1
(type ServiceInstaller) in the Installer class designer and also set itsServiceName
property in the designer.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.
Instead of creating your own class, select the service installer in the project installer and add an event handler to the Comitted event:
It will start your service only if startup type is set to automatic.
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.
Add the following class to your project.
The Setup Project will pick up the class and run your service after the installer finishes.
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:
(Every Installer has a ServiceProcessInstaller and a ServiceInstaller. Here the ServiceInstaller is called serviceInstaller1.)