Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / Visual Basic
Article

Using Single EXE to Deploy Multiple Windows Service

Rate me:
Please Sign up or sign in to vote.
4.00/5 (11 votes)
2 Sep 2007Public Domain1 min read 65.1K   713   32   10
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():
VB.NET
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.

VB.NET
' 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


Written By
Web Developer
Hong Kong Hong Kong
A .NET Programmer for 8 years, who wants to share the experience.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Robert detoi26-Nov-11 21:11
Robert detoi26-Nov-11 21:11 
GeneralSlight error in article Pin
radius3115-Jan-09 10:03
radius3115-Jan-09 10:03 
GeneralOther MSI Properties Pin
Discofunk17-Nov-08 10:57
Discofunk17-Nov-08 10:57 
Questionhow would the request get picked up? Pin
jeff94c22-Apr-08 18:07
jeff94c22-Apr-08 18:07 
AnswerRe: how would the request get picked up? Pin
WPKF22-Apr-08 20:51
WPKF22-Apr-08 20:51 
QuestionFileNotFoundException in .NET v1.1? Pin
jonashilmersson26-Oct-07 5:20
jonashilmersson26-Oct-07 5:20 
AnswerRe: FileNotFoundException in .NET v1.1? Pin
WPKF29-Oct-07 17:27
WPKF29-Oct-07 17:27 
GeneralRe: FileNotFoundException in .NET v1.1? Pin
jonashilmersson5-Nov-07 5:01
jonashilmersson5-Nov-07 5:01 
GeneralRe: FileNotFoundException in .NET v1.1? Pin
WPKF5-Nov-07 6:24
WPKF5-Nov-07 6:24 
GeneralThis will cut my build-script drastically - thanks! Pin
Torben Rohde28-Aug-07 4:14
Torben Rohde28-Aug-07 4:14 

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

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