Click here to Skip to main content
Licence Public Domain
First Posted 22 Aug 2007
Views 33,741
Downloads 319
Bookmarked 30 times

Using Single EXE to Deploy Multiple Windows Service

By | 2 Sep 2007 | Article
To set the Windows Service name in a Setup project instead of in Project Installer of the WIndows Service project

Introduction

My project required me to package one Windows Service EXE with different configuration files for different customers. This article will demostrate how to modify ProjectInstaller to support this requirement.

Background

My company has a Windows Service product. When deployed to different customers, we want to give only a simple Setup.exe (with *.msi) to the customers for easy deployment. We want to use a different name for the Windows Service being installed.

For example, for customer ABC, after setup, the name of the Windows Service would be "ABC Service".

We will package different configuration / data files using the same EXE.

Using the Code

In standard Windows Service Project Installer, the Service Name is set in Sub InitializeComponent():
Private Sub InitializeComponent()
' (other initialization code)
Me.ServiceInstaller1.ServiceName = "Service1"
'
End Sub  

The problem is that the ServiceName is hardcoded in the service.exe. Setup project will invoke the Project Installer in the service.exe and use Service1 as the Windows Service name. We want to choose the name in the Setup project instead.

Hence, we add some code to handle BeforeInstall and BeforeUninstall events.

    ' Force Setup project to specify the service name in CustomActionData 
    ' in "Install" Custom Install Action Developer should set the same name 
    ' in CustomActionData in "Uninstall" Custom Install Action
    Private Sub DoBeforeInstall(ByVal Sender As Object, 
        ByVal Args As System.Configuration.Install.InstallEventArgs) 
        Handles MyBase.BeforeInstall
        ' Context when Install
        If GetContextParameter("SERVICENAME") = "" Then
            Throw New Exception("SERVICENAME undefined")
        End If
        If Not Me.ServiceInstaller1 Is Nothing Then
            Me.ServiceInstaller1.DisplayName = GetContextParameter(
                "SERVICENAME")
            Me.ServiceInstaller1.ServiceName = GetContextParameter(
                "SERVICENAME")
        End If
    End Sub

    ' Developer should set the same name in CustomActionData in "Uninstall" 
    ' Custom Install Action
    Private Sub DoBeforeUninstall(ByVal Sender As Object, 
        ByVal Args As System.Configuration.Install.InstallEventArgs) 
        Handles MyBase.BeforeUninstall
        ' Context when Uninstall
        If GetContextParameter("SERVICENAME") = "" Then
            Throw New Exception("SERVICENAME undefined")
        End If
        Me.ServiceInstaller1.DisplayName = GetContextParameter("SERVICENAME")
        Me.ServiceInstaller1.ServiceName = GetContextParameter("SERVICENAME")
    End Sub

    Public Function GetContextParameter(ByVal key As String) As String
        Dim sValue As String = ""
        If Me.Context Is Nothing Then
            'WriteLog("Me.ServiceInstaller1 Is Nothing")
        End If
        Try
            sValue = Me.Context.Parameters(key).ToString()
            Return sValue
        Catch
            sValue = ""
            Return sValue
        End Try
    End Function

In Setup project property, specify the [ProductName]. It will be used as the Windows Service Name (e.g. DynamicService).

In Custom Action, specify the same SERVICENAME for custom action Install and Uninstall:

Screenshot - CustomerAction.jpg

Run the setup, a Windows Service called DynamicService is installed.

Screenshot - Servicesmsc.jpg

Points of Interest

  1. For setup of multiple services on the same machine, each setup should have a different [ProductCode] and [UpgradeCode].
  2. You may also change the Windows Service description using this technique.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

WPKF

Web Developer

Hong Kong Hong Kong

Member

A .NET Programmer for 8 years, who wants to share the experience.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberRobert detoi21:11 26 Nov '11  
GeneralSlight error in article Pinmemberradius31110:03 5 Jan '09  
GeneralOther MSI Properties PinmemberDiscofunk10:57 17 Nov '08  
Questionhow would the request get picked up? Pinmemberjeff94c18:07 22 Apr '08  
AnswerRe: how would the request get picked up? PinmemberWPKF20:51 22 Apr '08  
QuestionFileNotFoundException in .NET v1.1? Pinmemberjonashilmersson5:20 26 Oct '07  
AnswerRe: FileNotFoundException in .NET v1.1? PinmemberWPKF17:27 29 Oct '07  
GeneralRe: FileNotFoundException in .NET v1.1? Pinmemberjonashilmersson5:01 5 Nov '07  
GeneralRe: FileNotFoundException in .NET v1.1? PinmemberWPKF6:24 5 Nov '07  
GeneralThis will cut my build-script drastically - thanks! PinmemberTorben Rohde4:14 28 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 2 Sep 2007
Article Copyright 2007 by WPKF
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid