Creating a Project Installer for a Windows Service
I recently had to create a Windows Service for a project I’m currently working on. Rather than installing/uninstalling using InstallUtil, I decided to create a project installer to do the work for me. It took a little bit of digging to gather all of the necessary steps, so here is what I did to get up and running.
- Create your Windows Service project and Windows Service.
- Navigate to the designer for your Windows Service. Under the properties for your service you should see (Name) and ServiceName. Fill in both of these properties with your desired names. You will need to reference these later.
- In the designer for your Windows Service, right-click and click Add Installer. You should now have a ProjectInstaller.cs file, along with a designer file for the installer.
- Navigate to the designer for your new project installer. You should see two components, a Service Process Installer and a Service Installer. Under the Service Installer properties, you will need to add a DisplayName, and provide the ServiceName of the service you will be installing. The ServiceName must match the (Name) property of the service.
- Build your Windows Service project. Now in the same solution, add a new Setup Project. This will provide the Wizard interface for the installation process.
- After adding the project, you need to add the primary output of your Windows Service to the Setup Project. Do this by right-clicking on the Setup Project -> Add -> Project Output. Select your Windows Service project from the drop down, highlight Primary Output and click OK.
- Up to this point the installer will install the correct files, but it doesn’t know what to do with the files. We can add custom actions to do the service installation work. Right-click on the Setup Project -> View -> Custom Actions. For a Windows Service we need to add actions for Install, Rollback, and Uninstall. Right-click each of these and click Add Action. Double click Application Folder and select the Primary Output from your Windows Service project.
- You can now build your Setup Project. The output of this build will be two setup files which are used to install your new Windows Service