Good Morning, Nick. Thanks for using Microsoft Newsgroup Service. My name
is Hongye Sun [MSFT] and it is my pleasure to work with you on this issue.
Do you mean that, in your .NET service project, you have two service
classes with different service names?
They are installed in two different ServiceProcessInstallers, so that they
could be run in different processes with different security context?
You want two different configuration files for them because they have
different configuration contents. You are asking for the easiest way to
control the configuration files?
Please correct me if my understanding is incorrect.
In my opinion, the way you mentioned to use "service name".exe.config is
the best and easiest way for your situation. It has the benefits of easily
extending for more services and keeping multiple configurations transparent
to your service class. In the following section, I will focus on how to
implement this solution.
1. Create a new service base class named "ConfigServiceBase", which
inherits from ServiceBase class and with a property "Config". The code
looks like:
public class ConfigServiceBase : ServiceBase
{
public Configuration Config
{
get
{
return GetConfiguration(this.ServiceName);
}
}
public static Configuration GetConfiguration(string serviceName)
{
// Get the application configuration file path.
string exeFilePath = System.IO.Path.Combine(
Environment.CurrentDirectory, serviceName + ".exe.config");
// Map to the application configuration file.
ExeConfigurationFileMap configFile = new
ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath;
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);
return config;
}
}
The GetConfiguration static method first truncate the configuration file
path and then load the configuration file into Configuration type. In order
to use Configuration class, please add reference to System.Configuration
into project.
2. Implement Service classes inherited from ConfigServiceBase, and use
Config property to get configuration information. The code looks like:
public partial class Service1 : ConfigServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
string name = this.Config.AppSettings.Settings["name"].Value;
// To use name here
}
protected override void OnStop()
{
}
}
In this way, the multiple configuration file logic is transparent to your
service class.
3. Add a Service1.exe.config file into the project and make it copy to
output folder in every build.
If there are more services, we just need to add more service configuration
files and don't need to change any existing code.
Please have a try of the solution and let me know the result. If you feel
this solution is not suitable for your business requirement, please don't
hesitate to tell me and let me know more about your project. Let us try to
find some better solutions for you.
Have a nice weekend.
Regards,
Hongye Sun (
[email protected], remove 'online.')
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: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day 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 or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.