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

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

}

}

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 *

Loading...