How To Install Windows Service Without Installutil Exe

Posted onby

Home » c# » Install a.NET windows service without InstallUtil.exe Install a.NET windows service without InstallUtil.exe Posted by: admin November 13, 2017 Leave a comment. This is a base service class (ServiceBase subclass) that can be subclassed to build a windows service that can be easily installed from the command line, without installutil.exe. This solution is derived from How to make a.NET Windows Service start right after the installation?, adding some code to get the service Type using the calling StackFrame.

22 Nov 2007CPOL

Installutil.exe (Installer Tool); 6 minutes to read Contributors. All; In this article. The Installer tool is a command-line utility that allows you to install and uninstall server resources by executing the installer components in specified assemblies.

Never use the InstallUtil.exe utility that ships with the .NET SDK again.

Introduction

Installutil

Using the InstallUtil.exe utility that ships with the .NET SDK can be a real pain. It's rarely in the PATH so you probably have to hunt down the utility when you are working on QA and production servers as I do. Installing a Windows Service should be easier. In this short article, I'll show you a way to make your Windows Services install themselves without needing InstallUtil.exe at all.

Assumptions

Let's assume that your service project has a service installer, a service process installer and a class derived from System.Configuration.Install.Installer already. If not, check out Mahmoud Nasr's excellent article on Windows Service development, then come back here.

Install

Key Information

Thanks to Reflector for .NET by Lutz Roeder, it's easy to discover how the InstallUtil.exe utility does its job. After some setup, the InstallUtil.exe tool jumps to a method called InstallHelper in the ManagedInstallerClass in the System.Configuration.Install namespace. And what's really interesting is that the command line arguments passed to InstallUtil.exe as an array of strings are then passed directly to this helper method.

Well, this made me think, 'If all InstallUtil.exe does is call the ManagedInstallerClass' InstallHelper method, why can't my service executable do the same thing to install itself on command?' The little class below makes it simple to do just that.

Using the Code

Create a new CS file in your service executable project containing the following code. You may also need to add a reference to the System.Configuration.Install.dll from the Global Assembly Cache if you don't already have one.

Now you need to come up with some sort of convention for knowing when to invoke the installer. Below is just an example of how you might handle this in your Main() method, the entry point to your service. I like the convention of using -i or -install parameters to install the service and -u or -uninstall to uninstall it. I also like to use -c or -console to mean starting the application in a console rather than as a service. However, that's a topic for a different article.

How To Install Windows Service Without Installutil Exercises

Now, assuming my executable is named MyWinSvcHost.exe, I can invoke the installer by running:

Or to uninstall my service, I would use this:

Other Ideas

This little bit of code called the SelfInstaller is full of possibilities. You could pass parameters to the InstallMe method to pass on to the ServiceProcessInstaller in your program, for example. Perhaps the domain name, user name and password used to start your service could be passed all the way from your Main() method to the ServiceProcessInstaller. Cool, right? I thought you would like that.

C# Windows Service Installutil

History

  • 22 Nov, 2007 - Initial publication

Register Windows Service