Thomas Freudenberg

Confessions of a caffeine addict

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. Cool Awesome, Craig.

Why didn't Microsoft document that way? (Ok, they doc'd TransactedInstaller, but if you don't know where to search... sigh)

Comments

Anonymous said:

Is it possible to use this trick to install a Service on a shared server like on WebHost4Life ?
# October 12, 2003 6:13 PM

Anonymous said:

It works the same way installutil does. However, I doubt that WebHost4Life allows its customers to install OS services.
# October 12, 2003 6:51 PM

TrackBack said:

# January 7, 2004 11:02 PM

Anonymous said:

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.
# February 7, 2004 6:38 AM

Anonymous said:

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.
# April 16, 2004 4:32 PM

Anonymous said:

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?
# April 30, 2004 2:28 PM

Anonymous said:

Sorry, I've never created a MSI package on myself, so I don't know the answer.
# April 30, 2004 3:59 PM

Anonymous said:

But how do you install the service once complete. With installutil?
# April 30, 2004 5:14 PM

Anonymous said:

Pat, that's the trick of Craig's solution: to install the service using command line parameters instead of running installutil.
# May 1, 2004 7:04 AM

Anonymous said:

Anyone have a link to this article? It appears to be dead. Thanks!
# January 14, 2005 12:41 AM

Anonymous said:

You may contact Craig via his blog: http://pluralsight.com/blogs/craig/
# January 14, 2005 10:33 AM

Anonymous said:

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);
}
}

# January 25, 2005 2:33 PM

Anonymous said:

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
# February 18, 2005 12:46 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)