January 19, 2021
Hot Topics:

Creating a Windows Service in .NET

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Installing the Windows Service

Windows Services are different than normal Windows based applications. It is not possible to run a Windows Service by simply running an EXE. The Windows Service should be installed by using the InstallUtil.exe provided with the .NET Framework or through a deployment project such as a Microsoft Installer (MSI) file.

Adding an Installer

Having created the Windows Service will not be enough for the InstallUtil program to be able to install the service. You must also add an Installer to your Windows Service so that the InstallUtil, or any other installation program, knows what configuration settings to apply to your service.

  1. Switch to the design view for the service
  2. Right click and select Add Installer
  3. Switch to the design view of the ProjectInstaller that is added
  4. Set the properties of the serviceInstaller1 component
    1. ServiceName = My Sample Service
    2. StartType = Automatic
  5. Set the properties of the serviceProcessInstaller1 component
    1. Account = LocalSystem
  6. Build the Solution

The following code contained in the ProjectInstaller.cs source file was automatically generated by Visual Studio after having completed the steps above.

using System;using System.Collections;using System.ComponentModel;using System.Configuration.Install;namespace CodeGuru.MyWindowsService{  /// <summary>  /// Summary description for ProjectInstaller.  /// </summary>  [RunInstaller(true)]  public class ProjectInstaller : System.Configuration.Install.Installer  {   private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;   private System.ServiceProcess.ServiceInstaller serviceInstaller1;   /// <summary>   /// Required designer variable.   /// </summary>   private System.ComponentModel.Container components = null;   public ProjectInstaller()   {     // This call is required by the Designer.     InitializeComponent();     // TODO: Add any initialization after the InitComponent call   }   #region Component Designer generated code   /// <summary>   /// Required method for Designer support - do not modify   /// the contents of this method with the code editor.   /// </summary>   private void InitializeComponent()   {     this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();     this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();     //      // serviceProcessInstaller1     //      this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;     this.serviceProcessInstaller1.Password = null;     this.serviceProcessInstaller1.Username = null;     //      // serviceInstaller1     //      this.serviceInstaller1.ServiceName = "My Sample Service";     this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;     //      // ProjectInstaller     //      this.Installers.AddRange(new System.Configuration.Install.Installer[] {this.serviceProcessInstaller1, this.serviceInstaller1});}   #endregion  }}

Using InstallUtil to Install the Windows Service

Now that the service has been built you need to install it for use. The following instructions will guide you in installing your new service.

  1. Open a Visual Studio .NET Command Prompt
  2. Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode)
  3. Issue the command InstallUtil.exe MyWindowsService.exe to register the service and have it create the appropriate registry entries
  4. Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage
  5. In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services
  6. Start your service by right clicking on it and selecting Start

Each time you need to change your Windows Service it will require you to uninstall and reinstall the service. Prior to uninstalling the service it is a good idea to make sure you have the Services management console closed. If you do not you may have difficulty uninstalling and reinstalling the Windows Service. To uninstall the service simply reissue the same InstallUtil command used to register the service and add the /u command switch.

Page 2 of 3

This article was originally published on April 2, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date