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


 

2 Responses to “Installing .NET Services”

  1. .NET Windows Services tricks | Bob.Yexley.Net Says:

    [...] Creating a self-installing windows service [...]

  2. How To Run Faster Says:

    Nice read!. Your topic about Installing .NET Services needs more comments. I\’d like to spend me Monday nights reading about how to run faster

Leave a Reply

 




 
buy cheap Endep online buy cheapest Endep and Endep how to order Amitriptyline online without prescription buy Amitriptyline without rx from us pharmacy online Amitriptyline purchase purchase online prescription Amitriptyline without purchasing Amitriptyline online without prescription order generic Amitriptyline buy Endep no prescriptions how to order Endep online without prescription comprare Endep generico Endep online buy Endep in england buy cheap Endep online free consult Endep purchased online without prescription what is Endep Endep tabletten buy Endep no perscription cod Endep non rx fedex overnight free want to buy Anafranil in usa Aricept overdose buy discount Aricept on line Aricept buy Aricept online Aricept order buy Aricept pills Nizoral suppliers cheap order rx Nizoral canada Nizoral order Nizoral usa cod comprar Nizoral generico buy Nizoral 200 mg buy cheap Nizoral under without rx buy cheap Nizoral with dr. prescription Prozac 200 mg buy Lisinopril australia el Nizoral generico buy Lisinopril tablets without rx next day delivery Lisinopril with no script buy Lisinopril c o d purchase arimidex no scams order Arimidex order amex Arimidex canadian pharmacy buy Arimidex without doctor order rx free Arimidex buy Abilify with american express buy Abilify cod order Finpecia without a prescription cheap order rx Finpecia purchase Famvir no visa online without prescription buy Famvir paypal without rx buy Famvir without Famvir without prescription medications Famvir without persription purchase Famvir online no membership overnight shipping Famvir no physician approval Amitriptyline purchase order Amitriptyline free next day airAmitriptyline on line Valtrex no rx needed cod accepted buy next day Fluoxetine buy Fluoxetine line purchase Fluoxetine cod delivery buy Fluoxetine 20 mg purchase Fluoxetine buy cheap Fluoxetine under without rx purchase Fluoxetine visa without prescription Fluoxetine by mail Fluoxetine for pets Fluoxetine 10mg Amitriptyline purchase online how to order Famvir online without a rx Prozac Amitriptyline no perscription Famvir next day purchase Anafranil no prescription cheap buy cheapest Anafranil next day delivery on Anafranil saturday Anafranil buy Anafranil buy Anafranil overnight where to purchase Anafranil no prescription no fees Anafranil drug buy Lisinopril where Lisinopril buy on line order Lisinopril online no membership overnight shipping no prescription required for Lisinopril order maxalt cash on delivery Maxalt from canada buy cheapest Maxalt purchase Maxalt visa without prescription buy Maxalt uk Maxalt 10 mg purchase Maxalt no visa online without prescription purchase generic Lisinopril online Lisinopril no prescription to buy maxalt order online no membership overnight how to get a arimidex rx Aricept without a perscription fedex Aricept overnight without a prescription Donepezil prescription order buy genuine Aricept Aricept uk sales prescription Proscar buy Proscar pay cod Maxalt apotheke buy no perscription Proscar purchase Proscar online buy cheap fedex Prednisone maxalt with no presciption buy Maxalt shipped cod Proscar online Prednisone order buy Amitriptyline with amex Accutane 40 mg delivered overnight buy brand Accutane 40 mg Accutane 40 mg orderd online without prescription where to purchase generic Accutane 40 mg online without a rx how to get 40 mg Accutane without next day delivery 40 mg Accutane with no script buy Amitriptyline online Accutane 40 mg online no prescription buy Accutane 40 mg online us pharmacy Accutane 40 mg best buy buy Accutane 40 mg c o d order 40 mg Accutane cash on delivery no presciption 40 mg Accutane