Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Installing a .NET Windows Service Multiple Times

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Fri, Feb 27, 2015, 9:55 AM


Overview

I had a service that was developed to run against a database. Eventually, I needed to deploy this to our application server, but hit a wrinkle. We had only one application server to host the service, but multiple databases the service needed to run against. The solution was to install multiple instances of the service, each in its own folder and with its own configuration file. The key to getting this to work was to know how to install a .NET Windows service via installutil.exe and change the name of the service during installation. You do this by coding the service project installer to handle a custom command line argument. This article documents how to do this.

Procedure

1. Create a Project Installer for the service

2. Change the code as follows.

[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    private string _dividerBar;

    public ProjectInstaller()
    {
        InitializeComponent();
        _dividerBar = "=".PadRight(100, '=');
    }
    private void SetServiceName()
    {
        WriteLog("Setting Service Name");
        var items = Context.Parameters;

        WriteLog(string.Format("Found the following {0} parameter(s)", items.Count));

        foreach (DictionaryEntry item in items)
        {
            WriteLog(string.Format("  {0} = [{1}]", item.Key, item.Value));
        }

        if (items.ContainsKey("ServiceName"))
        {
            var s = items["ServiceName"];
            WriteLog("ServiceName = [" + s + "]");
            // TODO: Change ACME_Jobs_Emailer to whatever your service is
            this.ACME_Jobs_Emailer.ServiceName = s;
            this.ACME_Jobs_Emailer.DisplayName = s;
        }

    }
    protected override void OnBeforeInstall(IDictionary savedState)
    {
        WriteLog(_dividerBar);
        WriteLog("Installing");
        SetServiceName();
        base.OnBeforeInstall(savedState);
    }
    protected override void OnBeforeUninstall(IDictionary savedState)
    {
        WriteLog(_dividerBar);
        WriteLog("Uninstalling");
        SetServiceName();
        base.OnBeforeUninstall(savedState);
    }
    private void WriteLog(string msg)
    {
        msg = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + "> " + msg;
        this.Context.LogMessage(msg);
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.