How to install a window service

In my last post I had shown you how to create a self hosted windows service.
In this post I am going to walk through the process of adding an installer in a window service and how to use it for installation of window service.

  1. Right click on SelfHostService project and add an installer class named as WindowServiceInstaller.cs
  2. A class with following code snippet will be generated by default
  3. namespace SelfHostService
    {
        [RunInstaller(true)]
        public partial class WindowServiceInstaller : System.Configuration.Install.Installer
        {
            public WindowServiceInstaller()
            {
                InitializeComponent();
            }
        }
    }
    
  4. To install this SelfHostService executable, which contains a class that extend ServiceBase, we will require a ServiceProcessInstaller
  5. MSDN description of the ServiceProcessInstaller class is
  6. Installs an executable containing classes that extend ServiceBase. This class is called by installation utilities, such as InstallUtil.exe, when installing a service application.

  7. Also we will require ServiceInstaller class. MSDN description of the ServiceInstaller class is
  8. Installs a class that extends ServiceBase to implement a service. This class is called by the install utility when installing a service application.

  9. Let us finish our tiny installer. A complete code would look like this
  10. namespace SelfHostService
    {
        [RunInstaller(true)]
        public partial class WindowServiceInstaller : System.Configuration.Install.Installer
        {
            private ServiceProcessInstaller process;
            private ServiceInstaller service;
    
            public WindowServiceInstaller()
            {
                process = new ServiceProcessInstaller();
                process.Account = ServiceAccount.LocalService;
                service = new ServiceInstaller();
                service.ServiceName = "Self Hosted Windows Service";
                service.Description = "This is a self hosted windows service and can accept messages on port 8082 through HTTP";
                Installers.Add(process);
                Installers.Add(service);
            }
        }
    }
    
  11. [RunInstaller(true)] attributes is used by installutil.exe to identify the correct installer
  12. Compile the project. From command prompt, navigate to the path on your machine where InstallUtil.exe is located.
  13. Run command >installutil SelfHostService.exe
  14. Above command will install a window service. Now open Service Control Manager and start the service.

Thats all for this post. In next post I will show you how to create a MSI Installer file using a set up project.

About sagar

With over 13 years of professional experience as a .NET developer, I have honed my expertise in various facets of the .NET framework and related technologies. My extensive background includes proficiency in WPF, Web APIs, WCF services, and Windows services, which has enabled me to deliver robust and scalable applications across different domains.

Leave a Reply

Your email address will not be published. Required fields are marked *