Reputation: 143
I'm using Visual Studio 2010 And C# 4.0. I have a project that exposes an interface to a webservice. It has an app.config that defines the bindings for the webservice. I want to expose this project as a library for other clients to use.
However, when I try to import this library project in a client project (say a console application), I get an error because it couldn't find any configuration file associated with the webservice.
Is there a way to use the app.config inside my library project, so that my clients can use it without having to define a config file of their own?
Upvotes: 3
Views: 2643
Reputation: 73311
First create a utility function like below. This function should be in a library or class which could be called from your web service (and of course your main project). Better to add reference to this library in your web service.
public static string GetAppConfigValue(string key)
{
return ConfigurationManager.AppSettings[key] ?? GetAppConfigValue(GetAppConfigFileName(), key);
}
private static string GetAppConfigValue(string appConfigFileName, string key)
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = appConfigFileName;
Configuration appConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
return appConfig.AppSettings.Settings[key].Value;
}
Now if you can call GetAppConfigValue(string)
from your main project, it returns the value of the cached app.config since it is its own configuration file. You can also call the public function from web service project when it would return the mapped configuration settings. The tricky part here is to properly supply the full path of the config file!
Upvotes: 0
Reputation: 2963
How about you change the library project a little bit:
After that any project use this library should be able to not worry about those settings.
Upvotes: 1