Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reading Configuration settings

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?

like image 464
Nevin Mathai Avatar asked Sep 11 '26 04:09

Nevin Mathai


2 Answers

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...

like image 148
Charles Bretana Avatar answered Sep 12 '26 19:09

Charles Bretana


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.

like image 27
NotMe Avatar answered Sep 12 '26 17:09

NotMe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!