views:

1024

answers:

4

Hi all,

I have a .NET 3.5 class library I built that reads an App.config file for values it needs. It can pull the config values just fine when I test it in Visual Studio. To test it, I just change the project to a console application and execute a method call.

I have the need to call this class library from many other .NET programs, and I want the class library to be self sufficient (I should be able to call it from any other program, and it should use its own config file, not know about any calling config file etc.).

I can add a reference to the dll (since I am still development I am using VS 2008, haven't thrown anything into the GAC yet) but the App.config that the class library is reading is from the calling program's App.config, not the class library's App.config.

The class library dll has it's config file in the same directory, so it should be able to find it just fine, and the calling application is named differently. I am using the standard key value pairs in the App.config (e.g. name of config file myClassLibrary.dll.config) and getting values out with the following line of code:

String myVal = ConfigurationSettings.AppSettings["myConfigSetting"];

Does anyone know how to fix this?

A: 

I believe app.config will always be used by the executable. Just drop it in that directory.

They would do that to ensure the dll can be shared and not have to share the same .config file.

You might be able to create a link from the executable .config file

<appSettings configSource="\lib\app.config">

Or change its name, i don't understand how you can have both app.config files in the same directory..don't they have the same name?

<appSettings configSource="\lib.app.config">
Glennular
won't they both be being renamed to assemblyname.config when the assembly is built?
Hamish Smith
the class library is separate from the calling application, so no. I can look at the config file, open it up and verify all the settings are correct.
Mario
looks like your correct. I'm used to web.config files, had to go check.
Glennular
if you dont want to link the files, you can add the values from your assembly.config into your exe.app.config
Glennular
What I want to do is be able to call this standalone class library (all configured and ready to go from its own config file) from multiple applications without modifying each calling application's config file. The key word here is standalone.
Mario
And you tried adding the <appSettings configsource> to the exe.app.config? That is how i link up secondary config file.
Glennular
I am not sure what you are saying - I don't want either app.config to link/know about one another
Mario
+3  A: 

An app domain in C# can have only one assembly level app.config file. See here on MSDN. An executable will always start up an AppDomain and by default look for a config file with name: EXECUTABLE_NAME.config. For example, SampleApp01.exe will look for SampleApp01.exe.config as its configuration file.

siz
The config file exists with the proper name, but for some reason when the class library is called its not finding it, but using the caller's app.config
Mario
That's the conflict. "ConfigurationSettings" will only look for the config file for the assembly that started the appDomain. It will never look at your separate assemblie's config file.
David
+1  A: 

you can place your configs in the machine.config file inside the framework folder by this way you can globally use your configuration in all .Net applications running in that machine,

Oscar Cabrero
A: 

I can't find a way to avoid getting the app.config for the calling dll/exe etc. The only way I have found is to use a hardcoded path and load it that way. Here is code I am using to do that:

Using System.Configuration;

...

public static KeyValueConfigurationCollection getAppSettingsFromAppConfig(String appConfigPath)
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = appConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = config.AppSettings;
KeyValueConfigurationCollection appsettings = section.Settings;
return appsettings;
}

You then have a collection of KeyValueConfigurationElement, which you can use .Value to get the string from config file with

Mario