installutil - windows service installation

  • Thread starter Thread starter Param R.
  • Start date Start date
P

Param R.

Hi all,

I have written a windows service and installed it using installutil and
everything is working fine. I now have a need to install a new "instance" of
this windows service on the same server. What I mean by this is that I will
copy the windows service to a new folder, modify the config file to point to
another database and then would like to install this using installutil on
the same server. The problem is that the service with that name already
exists under Services. Is there a way to tell installutil.exe to give it a
new name in services.msc?

TIA!
 
Hi Param R.,

To answer your question:
Is there a way to tell installutil.exe to give it a
new name in services.msc?

No. Because the InstallUtil doesn't actually do the installation, it just
calls the project installer class inside your assembly and let the
installer class do the job.

By understanding how it works, here is a way to change the service name
without re-compiling the project after its been distributed, please follow
the steps to make some changes to your service project:

1. Add an App.Config file to your project (if there isn't already one).
2. Add a setting called ServiceName into the App.Config file, so it looks
like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServiceName" value="CoolService"/>
</appSettings>
</configuration>

3. Add a new class called Util looks like this:

using System;
using System.IO;
using System.Configuration;
using System.Reflection;

internal static class Util
{
private static string serviceName = null;

/// <summary>
/// Extracts the Service Name from config file.
/// </summary>
public static string ServiceName
{
get
{
if (serviceName == null)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(
Assembly.GetExecutingAssembly().Location);

serviceName =
config.AppSettings.Settings["ServiceName"].Value;
}

return serviceName;
}
}
}

4. In your service class, add code to set the Service Name on the fly:

public partial class MyCSService : ServiceBase
{
public MyCSService()
{
InitializeComponent();

// Set the ServiceName on the fly.
this.ServiceName = Util.ServiceName;
}

....
}

5. In your ProjectInstaller class code, add code to set the Service Name to
the ServiceInstaller on the fly:

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();

// Here, serviceInstaller1 is the name of my ServiceInstaller.
this.serviceInstaller1.ServiceName = Util.ServiceName;
}
}

6. Re-compile your project.

Now when you call InstallUtil on your service EXE, the installer class will
extract the service name from the exe config file on the fly, and use that
name as the service name. In this sample, the name is "CoolService". So you
can copy the same EXE and Config file to different folders and by just
changing the Config file's setting, you can have multiple instances of the
same service app with different names.

Hope this helps. If you have any further questions regarding this question,
please don't hesitate to let me know.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you. That worked!

"Jie Wang [MSFT]" said:
Hi Param R.,

To answer your question:
Is there a way to tell installutil.exe to give it a
new name in services.msc?

No. Because the InstallUtil doesn't actually do the installation, it just
calls the project installer class inside your assembly and let the
installer class do the job.

By understanding how it works, here is a way to change the service name
without re-compiling the project after its been distributed, please follow
the steps to make some changes to your service project:

1. Add an App.Config file to your project (if there isn't already one).
2. Add a setting called ServiceName into the App.Config file, so it looks
like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServiceName" value="CoolService"/>
</appSettings>
</configuration>

3. Add a new class called Util looks like this:

using System;
using System.IO;
using System.Configuration;
using System.Reflection;

internal static class Util
{
private static string serviceName = null;

/// <summary>
/// Extracts the Service Name from config file.
/// </summary>
public static string ServiceName
{
get
{
if (serviceName == null)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(
Assembly.GetExecutingAssembly().Location);

serviceName =
config.AppSettings.Settings["ServiceName"].Value;
}

return serviceName;
}
}
}

4. In your service class, add code to set the Service Name on the fly:

public partial class MyCSService : ServiceBase
{
public MyCSService()
{
InitializeComponent();

// Set the ServiceName on the fly.
this.ServiceName = Util.ServiceName;
}

....
}

5. In your ProjectInstaller class code, add code to set the Service Name
to
the ServiceInstaller on the fly:

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();

// Here, serviceInstaller1 is the name of my ServiceInstaller.
this.serviceInstaller1.ServiceName = Util.ServiceName;
}
}

6. Re-compile your project.

Now when you call InstallUtil on your service EXE, the installer class
will
extract the service name from the exe config file on the fly, and use that
name as the service name. In this sample, the name is "CoolService". So
you
can copy the same EXE and Config file to different folders and by just
changing the Config file's setting, you can have multiple instances of the
same service app with different names.

Hope this helps. If you have any further questions regarding this
question,
please don't hesitate to let me know.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
I would check this out:

http://www.eggheadcafe.com/articles/20041204.asp

Do NOT concentrate (so much) on the Command Pattern or the MSMQ part.

Concentrate on the way Peter setup installing and uninstalling the services
from within the DotNet code itself.

.......

I have used Peter's (inspired) idea for years now and have never looked back
(to installutil)
 
Back
Top