.NET Windows Service Installer and Auto Start After Installation

It’s not very often that I need to build a Windows Service for a specific task, and it’s even more rare that I need to create an Installer (.msi) to install/uninstall the service. Well, the project I’m currently working on Virtual Router requires me to Install a Windows Service using a Setup Project, and Start the Windows Service immediately after installation.

Create Setup Project for Windows Service

Just create a Setup Project within your Solution in Visual Studio, then follow the below steps:

  1. Right-Click the Setup Project you just created
  2. Click “Add”, then “Project Output”
  3. In the dialog that appears select your Windows Service Project as Primary Output, then click OK.

That’s really all that’s required. When the resulting Installer is executed it will Install / Uninstall the Windows Service.

You can find a longer description of how to do this here:

http://support.microsoft.com/kb/317421

Auto Start Windows Service After Installation

Whether you use a Setup Project of the installutil tool to install your Windows Service, it can be very convenient if the service were to automatically start once installed. To do this all it takes is adding a couple lines of code to the ServiceInstaller that you have defined within your Windows Service Project to handle the “Committed” event, then use the ServiceController class to Start the service.

Here’s an example ServiceInstaller with the “Auto Start” code in place:

[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string strServiceName = "MyServiceName";

    public ServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        processInstaller.Account = ServiceAccount.LocalSystem;
        processInstaller.Username = null;
        processInstaller.Password = null;

        serviceInstaller.DisplayName = strServiceName;
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = strServiceName;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Auto Start the Service Once Installation is Finished.
        var controller = new ServiceController(strServiceName);
        controller.Start();
    }
}

Conclusion

These are two tips that for some reason had eluded me for a long time. Usually I just created a .bat file to call installutil and net start to install and start my services manually. In most cases that worked perfect since it was very rare that the service was installed on a new computer or updated to a newer version. However, while building an “End User Friendly” Installer that anyone can run, you need to automate the installation and start up of the Windows Service.

This is just the thing that I’m running into with the new Virtual Router open source project that I’m working on. The Virtual Router project utilizes Windows 7’s Virtual Wifi and Wireless Hosted Network API’s to turn any computer into a Wireless Access Point / Router. Look for the first release of this project soon!