views:

601

answers:

5

I have a C# class library A which has some configuration settings in its App.config I access them with

Method1()
{
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
}

But when I call Method 1() from my ASP Web project B, it cannot find the configurations settings in the Class library A

Any idea what is happening here?

+2  A: 

The config settings have to be copied to your web.config. Essentially there is only one default config file per project which the ConfigurationManager reads.

Chris Lively
A: 

It's looking for the configuration setting in your web project.

Justin Drury
+2  A: 

A library doesn't have its own configuration file. Configuration settings should be defined in the exe that uses that library

Thomas Levesque
+3  A: 

The entire configuration management structure created by .Net runtime, is process-specific. not assembly specific. This means that each running executable gets an app.config. A Web project gets a web,config (actually a web project can have multiple web.configs), but assemblies cannot have their own app.configs, they can have code to read the configuration settings in the config file for whatever process they are being referenced in (which use the assembly as a reference in a winforms app, then it can see config settings in the MyWinformsApplication.exe.config; Use the assembly in an ASP.Net web app, then it can see confiog settings in the web applications' web.config...

Charles Bretana
+2  A: 

I believe you can use OpenExeConfiguration to do this:

string exePath = "<full path and name of the app .exe file>";

System.Configuration.Configuration otherConfig = 
     ConfigurationManager.OpenExeConfiguration(exePath);

You could put the path of the other .exe in the web app's web.config (for instance, in the appSettings section), and read it from there, which would be better than hard-coding it here.

to view the appSettings in that config file:

AppSettingsSection otherAppSettings = otherConfig.AppSettings;

This MSDN page might help.

DOK