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

An interesting customer question came to my attention a few weeks ago. The customer had created a managed service, and was installing it with a custom name that was provided at install time by the user. I’ve seen several customer applications that do this. Upon uninstall, they were encountering a failure – this seemed bizarre, as during install time everything seemed to have gone well with no errors. What had happened here?

 

Let’s take a look at how Services get installed and how we can install a simple service with the name of our choice. From a very high level, you first create a service that you want to install. You then create an installer that specifies the actions you want to take place at install and uninstall time. Finally you install your component using the install utility (InstallUtil.exe). InstallUtil.exe is a tool that lets you install/uninstall server resources based on components in an assembly. This tool ships with the .NET Framework Redistributable. Ok, now it’s time to drill down and take a look at how to do all this…

 

The ServiceBase class

There are a few things that need to be understood before we get started. Let’s begin by taking a look at the actual service. All services must derive from the ServiceBase class.

 

The ServiceBase class has several protected methods that you can override in your derived class. For a service to be useful, you probably want to override at least the OnStart and OnStop methods.

 

OnStart is executed when a Start command is sent to the service by the Service Control Manager.

OnStop is executed when a Stop command is sent to the service by the Service Control Manager.

 

Below is the code snippet for a service that we can install. By default, a service has the AutoLog property set to true, which means that it will log an event log message whenever it is started or stopped. You can modify the OnStart and OnStop methods to do something useful.

 

public class SimpleService : ServiceBase

{

    public static void Main()

    {

        ServiceBase.Run(new SimpleService());

    }

    public SimpleService()

    {

        CanPauseAndContinue = true;

      // Here we are setting a service name to show you don't need a custom // name if you don't want one

        // This gets overriden in this example later

        ServiceName = "SimpleService";

    }

    protected override void OnStart(string[] args)

    {

       //This is where your service would do something useful when it starts

    }

    protected override void OnStop()

    {

        //This is where your service would do something useful when it stops

    }

}

<editorial notes>
This week, instead of posting Mon, Wed, Fri. The BCLTeam Weblog will post the 4 installment of Rob's article "Installing a Managed Service with a Custom Name." Tomorrow, we'll learn about the Installer class, ServiceProcessInstaller and ServiceInster classes.
</editorial notes>