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

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

  • Comments 6

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>

 

  • I've been trying to do something similar to this for quite a while; I wanted a generic service that I could install multiple times using differenct configuration files. I hoped to be able to start each instance and have it behave differently, based on how it had been installed. What I found, however, was that the service itself didn't know what it was installed as: didn't know its name. So, short of hand-typing in parameters for each instance, the different installations of the same service couldn't actually perform indepdently of one another.

    It seemed like a good idea at the time. It still, actually, sounds like it would be useful. Is there any way to pull it off?
  • Hi jdm, I think you should be able to do this.  The tricky part is that you need to make sure the  ServiceName property gets set correctly and persisted somewhere so you know which configuration data you have is related to which service.  The installer doesn't remember all this for you, so you have to do it yourself.  
  • Are non-letter characters such as the '@' sign or an asterisk legal in a custom name for a managed service?

    Thanks
  • Where are the links to other parts of this series?
  • Hi Ann, yes you can use the '@' sign or '*' in the custom name.  Invalid characters include forward and backward slashes as well as anything with ASCII decimal value of less than 32.
  • Hey Ravi, the rest of the series is posted now on this blog.
Page 1 of 1 (6 items)