Installing a Managed Service with a Custom Name (Part 2 of 4) [Robert Villahermosa]

The Installer class

Now that we have our simple service to install, let’s take a look at how we can install it. 


Custom installers in the .NET Framework all derive from the Installer class.  There are several methods that you can override in this class, but we won’t go into detail about these here except for one property called Installers.  This property is an InstallerCollection of all the separate installers that this instance of this type contains.  In order to install an application, you create your own ProjectInstaller class that derives from Installer, and then add all the installers to the InstallerCollection.



The ServiceProcessInstaller and ServiceInstaller classes

A service application can consist of multiple services.  The ServiceProcessInstaller class installs a service application and does work common to all services in it.  You need one ServiceProcessInstaller per service application.  The ServiceInstaller class installs a class that extends ServiceBase to implement a service, so you need one ServiceInstaller per service.  


Confused?  Let’s look at the code sample below to clarify things.



public class ProjectInstaller : Installer


    private ServiceInstaller serviceInstaller;

    private ServiceProcessInstaller processInstaller; 


    public ProjectInstaller()


        processInstaller = new ServiceProcessInstaller();

        serviceInstaller = new ServiceInstaller();

        // Service will run under system account

        processInstaller.Account = ServiceAccount.LocalSystem;

        // Service will have Start Type of Manual

        serviceInstaller.StartType = ServiceStartMode.Manual;

        // Service will have the following name (optional) this just shows

        // you don't need to have a custom name, you can omit this though

        serviceInstaller.ServiceName = "SimpleService";

        // Hook up some custom events prior to the install and uninstall

        BeforeInstall += new InstallEventHandler(BeforeInstallEventHandler);

        BeforeUninstall += new InstallEventHandler(BeforeUninstallEventHandler);    









As seen above, one ProjectInstaller is created that derives from Installer.  Then, a processInstaller and serviceInstaller are added to this instance.  I’m setting some properties above as defaults, specifically we’re saying we want our sample service to run using the LocalSystem account and we want to start it manually.  I almost forgot to mention, you need an attribute RunInstallerAttribute set to true.  This gets read by InstallUtil.exe to determine what installer components in a specified assembly get executed.




p class="MsoNormal" style="MARGIN: 0in 0in 0pt">

<editorial notes>
Tomorrow, we'll cover how to specify a custom name and how to persist the data.
</editorial notes>

Comments (6)

  1. Anonymous says:

    Wouldn’t it be better to just override OnBeforeInstall and OnAfterInstall instead of hooking up the events?

  2. Anonymous says:

    I’m hoping you’ll cover how to set the long description for a service – there doesn’t seem to be any managed way to do this, in v1.1 at least.

  3. robvi says:

    Hi Eric, yes you could override OnBeforeInstall and OnAfterInstall instead of hooking up the events.  I thought it would be nice to show this way though.  When overriding the methods in the base class though, you have to remember to call the base class’ OnBeforeInstall/OnAfterInstall to make sure registered delegates are notified of the events.

    Thanks for pointing this out for other people who might have been confused by this!

  4. robvi says:

    Hi James,

    You know what?  To be honest with you I’m not sure of a good way to do this in v1.1 aside from tweaking some registry keys.  In v2.0 however, we’ve added a new property to ServiceInstaller to do this.  Set ServiceInstaller.Description to the long description that you want (by long description I’m assuming you’re talking about the one that appears in the services.msc).  That should do the trick.

    Thanks for asking,


  5. Anonymous says:

    This seems to be the only blog in the world that does not syndicate the content of the articles, only the abstract.

    This is very annoying, any chance this will be changed?

  6. Anonymous says:

    Thanks Rob. I guess we’re stuck with the Registry hack or calling ChangeServiceConfig2

Skip to main content