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.

 

[RunInstallerAttribute(true)]

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);

        Installers.Add(serviceInstaller);

        Installers.Add(processInstaller);

    }

.

.

.

   

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>