How to Install Window Services Using WiX

In this article, I will show you how to install, and control Windows Services in WiX step by step. It can be done by using Wix ServiceInstall and ServiceControl elements that are available from Wix Tool.

Add a Windows Service with WiX ServiceInstall

First, you have to define a Component for a File that will be used for Windows Service under the Directory Element.

<Component Id="SimpleServiceComponent" Guid="{PUT-GUID-HERE}" >
         <File Id="SimpleService.exe" Name="SimpleService.exe"  KeyPath='yes'/>
                ...           
</Component>

ServiceInstall is an element to add a Windows Service in Wix, and this can be located in the same Component with the File.

<Component Id="SimpleServiceComponent" Guid="{PUT-GUID-HERE}" >
        <File Id="SimpleService.exe" Name="SimpleService.exe"  KeyPath='yes'/>
            <ServiceInstall Id="SimpleService"
                            Name="SimpleService"
                            DisplayName="SimpleService"
                            Description="Simple Example Service"
                            Start="auto"
                            Type="ownProcess"
                            ErrorControl="normal"
                            Account="LocalSystem"
                            Vital="yes" />
</Component>

Here, I’ve used the LocalSystem account for this service to run under, but you can also use another user account with Account and Password attributes.

I also added Component Id to Feature Section so that the Windows Installer installs the Service component.

<ComponentRef Id="SimpleServiceComponent" />

Run:

I’ve built this Wix project in Visual Studio, ran the MSI package, and confirmed that the Windows Service was Installed.

However, without additional service control settings, I think it was not a complete solution yet.

I wanted Service running, but I found the Service was not running after the installation was completed. When I uninstalled the program, I could also notice that the windows service was still not uninstalled.

Control State of a Windows Service with Wix ServiceControl

You can use the ServiceControl element to start, stop, and remove the Windows service defined in the Component.

In this example, I set the Service to start during installation and to remove the service during the uninstallation of the application.

<ServiceControl Id="SimpleServiceControl" Name="SimpleService" Start="install" Stop="both" Remove="uninstall" Wait="yes"/>

I could add the Windows Service File, ServiceInstall, and ServiceControl elements in a single Component as a set.

Run:

Running built MSI, I could verify the Windows Installer Package created the SimpleService and started the Service during installation. In addition, the service was uninstalled when the application was uninstalled. That is the perfect cycle of service to achieve with this installer.

You may be interested in reading the articles:

How to Create Wix Project in Visual Studio 2019

How to add all files from a folder to MSI Installer using WIX

Leave a Comment