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.

Right click on SelfHostService project and add an installer class named as WindowServiceInstaller.cs A class with following code snippet will be generated by default namespace SelfHostService { [RunInstaller(true)] public partial class WindowServiceInstaller : System.Configuration.Install.Installer { public WindowServiceInstaller() { InitializeComponent(); } } } To install this SelfHostService executable, which contains a class that extend ServiceBase, we will require a ServiceProcessInstaller MSDN description of the ServiceProcessInstaller class is Installs an executable containing classes that extend ServiceBase. This class is called by installation utilities, such as InstallUtil.exe, when installing a service application. Also we will require ServiceInstaller class. MSDN description of the ServiceInstaller class is Installs a class that extends ServiceBase to implement a service. This class is called by the install utility when installing a service application. Let us finish our tiny installer. A complete code would look like this 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); } } } [RunInstaller(true)] attributes is used by installutil.exe to identify the correct installer Compile the project. From command prompt, navigate to the path on your machine where InstallUtil.exe is located. Run command >installutil SelfHostService.exe 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.