How to create an installer for a .net Windows service app using Visual Studio

Written by Renato Ivanescu · September 20th, 2022

#SERVICES

When we talk about a Windows Service, we're referring to an application that runs on its own Windows session and does not show any user interface. Windows Services are a great option when you want to carry out an installation without interfering with the user working on the computer.

We can use Visual Studio to easily create a service by starting a project called Windows Service. But let's not forget that we also need an installation package to be able to install the application service.

Fortunately, we can create both the application and the installer using Visual Studio IDE (integrated development environment). We will show you how in this article.

First, we will be discussing how to add installation components to a service, and then we will move on to creating an installer package for your service application using Visual Studio.

Let's jump right in.

How to add installation components to a service?

For this tutorial, we created a simple Windows Service project that adds a new text file on the system when it starts. We renamed the component class included in the project template: from Service1.cs to MyService.cs. From now on, we will refer to it as MyService.

Windows Service project

As mentioned above, we're first going to add installation components and customizations to our service.

When installing an application service, you also need to install resources associated with it to make sure the service runs on the system. To do this, use the installation components that Visual Studio offers.

Components register the service on the system and let the Services Control Manager know that your service exists.

1. To add an installer component to your service to handle the registration details, open MyService.cs -> right-click in the designer - > Add Installer.

Add Installer component

2. Now, after adding the installer, you should find the ProjectInstaller class in solution explorer.

Project Installer class

NoteWithin ProjectInstaller (which is called by the install utility), there are two objects that are automatically generated: the serviceinstaller object created for our service (if you want to add more services, a serviceinstaller object must be created for each of them within the class constructor), and the serviceprocessinstaller object created for the executable.

You can see these two objects in the designer if you double-click on ProjectInstaller.

Project Installer - Design

3. To manage the options for your service, click on serviceInstaller1, then go to Properties.

4. You can change the start type of your service. We set it to Manual so that the service starts manually. You can also change it to Automatic if you want it to start with the operating system at system start-up. In our case, we set the service name to MyService. This property is used by the system to identify the service. We also added a description to our service.

Service description

5. Go to serviceProcessInstaller properties and change the Account from User to LocalService. This will make sure the system won’t ask for a valid user and password when you install the service.

Service properties - account

How to add the Microsoft Visual Studio Installer Projects extension?

After you add the installer component and customize your service, you can create the installer package for your service application. To do this, you first have to add the Microsoft Visual Studio Installer Projects extension to Visual Studio.

There are two available options to add the Microsoft Visual Studio Installer Projects extension:

  1. In Visual Studio, go to Extensions -> Manage extensions. Look for the extension in the Online section and download it. It will be automatically installed after you close the IDE. When you reopen Visual Studio, the extension will be ready to use.
  2. Go to the Visual Studio Marketplace, download the extension and install it. Make sure that the extension version is suitable for your Visual Studio version. We used Visual Studio 2022 for this tutorial and installed this version of the extension. After you install the extension, reopen Visual Studio.

How to add a Setup Project to your service application?

Once we have the extension, we can add a Setup Project to the service application. Here's how:

1. Create a new Setup Project inside the solution to help you pack the MSI based on your Windows service application.

2. To add the Setup Project, right-click on the project solution -> Add -> New Project.

Add New Project Solution

3. Choose Setup Project from the Add new project dialog and give it a name. The name of our Setup Project is MyServiceInstaller.

Setup Project Name

4. You should be able to see the Setup Project in Solution Explorer after creating it.

Setup Project Solution Explorer

5. To see how the Setup Project is organized, right-click on the Setup project -> View -> File System to open the file system editor where you should find three different folders.

File System Editor

How to add the Project output?

We need to add a project output to specify the service we are creating the installer for. Follow the next steps to add the project output for your Windows service.

  1. Right-click on the Application Folder -> Add -> Project Output.
  2. Choose the Primary output option from the opened dialog.
Primary Output Option

How to add custom actions to install the service?

In order to install the service when running the application installer, we have to add an install custom action which contains the Primary output. To achieve this:

1. Right-click on MyServiceInstaller -> View -> Custom Action.

2. Then, right-click on the Install folder under Custom Actions.

3. Now, select Add Custom Action.

Add Custom Action Install

4. In the opened dialog, look in the Application Folder and select Primary output.

Application Folder Primary Output

What if you need to uninstall the service?

To uninstall the service when uninstalling the application, we need to create an uninstall custom action.

  • Go to the Uninstall folder from Custom Actions and follow the same steps as you did for the Install folder.
  • After this, you will see your custom actions created like in the image below.
Add Custom Action Uninstall

How to Build and Install the Setup Project?

Build the Setup Project and browse to the MSI file generated after the build. We should find it in the Debug or Release folder (depending on the build mode) of the Setup Project. Run the setup and look for your service in Service Control Manager after installation.

MyService Control Manager

We can identify the service (MyService) along with the description we set. The service is running as we started it manually. If you set the Start type to automatically, it should be running without you having to start it.

You can also try a friendly GUI packing software like Advanced Installer to manage service customizations better. You only need to add your service to a project and take advantage of options like User Account, Start Type, Service Parameters and many others.

Check our How to install a service tutorial to see more about how to install a service using Advanced Installer.

Conclusion

In addition to creating a service, Visual Studio offers the possibility to create an installer for that application service. Things look quite simple if you want to install a service which doesn’t require a complex setup, but it could become difficult if you have several services to install, especially if they require advanced configurations.

Do you have questions or topics you'd like us to cover? Let us know in the comments.

Subscribe to Our Newsletter

Sign up for free and be the first to receive the latest news, videos, exclusive How-Tos, and guides from Advanced Installer.

Comments: