dynamic windows service name
Hi all,
I am trying to install a service using dynamic naming. I wrote values in the
configuration file, and trying to assign one of those values to a string,
and using this string as the name of the installer
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="StringName" value="StringValue" />
</appSettings>
</configuration>
//Code in the service installer
str=System.Configuration.ConfigurationSettings.App Settings["StringName"];
this.serviceInstaller1.ServiceName=str;
when using the installutil tool, I get the following exception:
An exception occurred during the Install phase.
System.ArgumentException: Must specify value for source.
Usually, if I write the string value in a normal text file, and read the
value from the file while installing (instead of reading from the
appSettings element) I don't get any exceptions and it works fine..
So what could be the reason then? and normally how can you set a dynamic
service name using the configuration file?
Thank you for your help
Re: dynamic windows service name
Hello
The config file should have the name filename.exe.config, and must be in the
same directory as the executable file. Since you are using .NET framework's
installutil.exe to install your service. In this case, you must name the
file installutil.exe.config and put it in the .NET framework folder under
the system directory. This is not practical of course.
Best regards,
Sherif
Re: dynamic windows service name
Sherif has told you why that won't work. What I do for that is:
private string ServicePrefix {
get {
Assembly currentAsm = Assembly.GetExecutingAssembly();
FileInfo fi = new FileInfo(currentAsm.Location);
DirectoryInfo runFolder = fi.Directory;
string prefix = "";
foreach (FileInfo extFi in
runFolder.GetFiles("ServiceName.*")) {
prefix =
extFi.Extension.Replace(".","").ToUpper();
}
return prefix;
}
}
public ProjectInstaller() {
// This call is required by the Designer.
InitializeComponent();
string serviceName = this.ServicePrefix
+ this.serviceInstaller1.ServiceName;
Console.Write("ServiceName is " + serviceName + " (enter to
continue) ");
Console.ReadLine();
this.serviceInstaller1.DisplayName = serviceName;
}
This looks for a file called ServiceName.* in the same folder as the
Service's exe and whatever the * translates to is used as the prefix
of the service name as set at design time.
This allows me to have multiple copies of my service running on the
same machine.