Installing .NET Services

I've been trying to figure out how install and uninstall a .NET service.

 

There is a managed wrapper for the SCM in the form of the ServiceController class, which is very nice for doing things with installed services, but it has no support for installing or uninstalling a service.  It doesn't even support determining whether a service is installed or not - you have to get the entire service list and iterate through it yourself.

 

The framework provides installer support in the form of the System.Configuration.Install namespace, which has a ProjectInstaller and a ServiceProcessInstaller.  Seems simple enough - you mark these up with the [RunInstaller] attribute, and use the InstallUtil.exe utility to install the service.

 

There are a few problems with this scheme.  First, this means a service isn't self-installing.  Users are used to being able to invoke “servicename.exe -i” to install a service.

 

Craig Andera figured out how to make a service self-installing; the code on his page doesn't quite compile; here's a version that does:



// The main entry point for the process
static void Main(string[] args)
{
  if (args.Length > 0)
  {
    if (args[0] == "-i")
    {
      TransactedInstaller ti =
new TransactedInstaller();
      ProjectInstaller pi =
new ProjectInstaller();
      ti.Installers.Add(pi);
      string basePath = Assembly.GetExecutingAssembly ().Location;
      String path = String.Format("/assemblypath=\"{0}\"", basePath);
      String[] cmdline = {path};
      InstallContext ctx =
new InstallContext(Path.ChangeExtension(basePath, ".InstallLog"), cmdline);
      ti.Context = ctx;
      ti.Install (
new Hashtable());
    }
    else if (args[0] == "-u")
    {
      TransactedInstaller ti =
new TransactedInstaller ();
      ProjectInstaller pi =
new ProjectInstaller ();
      ti.Installers.Add (pi);
      String path = String.Format("/assemblypath=\"{0}\"", Assembly.GetExecutingAssembly ().Location);
      String[] cmdline = {path, servicename};
      InstallContext ctx =
new InstallContext(Path.ChangeExtension(basePath, ".UninstallLog"), cmdline);
      ti.Context = ctx;
      ti.Uninstall (
null );
    }
  }
else
  {
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun =
new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  }
}


However, to me, it seems there's a lot of overhead here for a fairly simple operation (especially considering that there's also a separate file containing the ProjectInstaller and it's ServiceProcessInstaller).  Also, if you need to pass install-time parameters to your service, such as specifying it's service name (a fairly common requirement), then you have to pass this to the ProjectInstaller and have it update all the various installer classes that you need for installing event logs and whatnot..


So I ended up using some code written by Sachim Nigam that wraps the Service Control Manager and lets you call it directly. 


Equivalent code ends up looking like this:



// The main entry point for the process
static void Main(string[] args)
{
  if (args.Length > 0)
  {
    if (args[0] == "-i") 
    { 
      SvcInstaller.ServiceInstaller si =
new SvcInstaller.ServiceInstaller();
      si.InstallService(Assembly.GetExecutingAssembly().Location, "MyService", "This is my service.");
    }
    else if (args[0] == "-u")
    {
      SvcInstaller.ServiceInstaller si =
new SvcInstaller.ServiceInstaller();
      si.UnInstallService("MyService");
    }
  }
else
  {
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun =
new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
  }
}


It doesn't let you do anything that you can't do with the .NET Installer mechanism, but it's a lot simpler, faster, and more understandable.


(On the same topic, having a “-d” option which invokes Service1 and directly calls the OnStart method instead of calling ServiceBase.Run makes it easy to debug your service by specifying -d on the command line and just stepping into it).