Self-Installing .NET Windows Service
Did you ever write a .NET Windows service? What I really dislike about it is that though the framework provides the System.ServiceProcess.ServiceInstaller
class, you still have to use the external installutil tool.
Well, at least until today.
After some googling I found Craig Andera’s article HowTo: Write a Self-Installing Service. Only a few lines of code are needed to get your service registering itself. Awesome, Craig.
Why didn’t Microsoft document that way? (Ok, they doc’d TransactedInstaller
, but if you don’t know where to search… )
Comments
Julien CHEYSSIAL
Is it possible to use this trick to install a Service on a shared server like on WebHost4Life ?
Anonymous
Anyone have a link to this article? It appears to be dead. Thanks!
Craig
Sorry - when I moved my blog the old links went dead. You can find the article here [1] now. It’s a wiki, so if there are any errors, you should feel welcome to fix them. :)
[1] http://pluralsight.com/wiki/default.aspx/Craig/SelfInstallingService.html
Anonymous
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
[RunInstallerAttribute(true)]
public class MyProjectInstaller: Installer{
private ServiceInstaller serviceInstaller1;
private ServiceInstaller serviceInstaller2;
private ServiceProcessInstaller processInstaller;
public MyProjectInstaller(){
// Instantiate installers for process and services.
processInstaller = new ServiceProcessInstaller();
serviceInstaller1 = new ServiceInstaller();
serviceInstaller2 = new ServiceInstaller();
// The services run under the system account.
processInstaller.Account = ServiceAccount.LocalSystem;
// The services are started manually.
serviceInstaller1.StartType = ServiceStartMode.Manual;
serviceInstaller2.StartType = ServiceStartMode.Manual;
// ServiceName must equal those on ServiceBase derived classes.
serviceInstaller1.ServiceName = “Hello-World Service 1”;
serviceInstaller2.ServiceName = “Hello-World Service 2”;
// Add installers to collection. Order is not important.
Installers.Add(serviceInstaller1);
Installers.Add(serviceInstaller2);
Installers.Add(processInstaller);
}
}
Thomas Freudenberg
You may contact Craig via his blog: http://pluralsight.com/blogs/craig/
Thomas Freudenberg
Sorry, I’ve never created a MSI package on myself, so I don’t know the answer.
Thomas Freudenberg
Pat, that’s the trick of Craig’s solution: to install the service using command line parameters instead of running installutil.
Anonymous
But how do you install the service once complete. With installutil?
Thomas Freudenberg
It works the same way installutil does. However, I doubt that WebHost4Life allows its customers to install OS services.
Maran
for poeple who stumbled on this article and were worrying about the broken hyperlink of Craig’s blog, here’s a working link.
http://sites.google.com/sit…
pretty nice trick though.
Anonymous
Once the self installing code is written, can this project be referenced in a setup project (msi)? Is there any special steps to take for creating the MSI?
Charlie Kindel
CraigBlog’s docs are not quite right for .NET 1.1. I had to do the following:
First, the main checks for args.Length > 1. This should be args.Length > 0.
Next, ti.Context is a property, not a method, so ti.Context(ctx) should be ti.Context = ctx.
Last, it’s not clear how to deal with normal service startup in the sample. I added an else to the if (/install) else if (/uninstall) clause where I put my “I’m running as a service” code.
Anonymous
I ran in to the same isues as Charlie with Framework 1.0. I corrected the problems but still could not get the service to install. I’m going back to installing manually.
Leave a Comment
Your email address will not be published. Required fields are marked *