Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to retrieve key from App.Config

Tags:

asp.net

In my WAP project I have a reference to another C# Project (non-web). In that C# project I have an app.config and I renamed it to [projectName].config

I then tried to grab a key from my [project].config from some code within my C# Project and it can't find it:

return ConfigurationManager.AppSettings["somekey"]

So I am wondering why it can't read my app.config. I would think that ConfigurationManager would be able to read keys form the Web project's web.config AND my [projectName].config (app.config) file that is in my referenced project as well?

like image 794
PositiveGuy Avatar asked Dec 14 '22 00:12

PositiveGuy


2 Answers

ConfigurationManager will read the configuration of the application's host. In your case, the web app is the application's host - therefore, calling ConfigurationManager will read from your web.config - even if you make calls to ConfigurationManager from a referenced dll.

If your web application references a dll, then the dll should get its configuration from the web.config. This is the best thing for the application. If the dll is referenced by multiple types of applications, the settings in the config may need to be different.

like image 152
Gabriel McAdams Avatar answered Feb 09 '23 13:02

Gabriel McAdams


Try putting this in your web.config:

<appSettings file="externalSettings.config"/>

externalSettings.config is the other config file you want to use.

For a non web assembly, it looks for AssemblyName.dll where AssemblyName corresponds to your assembly name. Ex: if your assembly name is Com.MyProject.Library1.dll, then the config file it's looking for is Com.MyProject.Library1.dll.config.

UPDATE: I did some more research on this, and as noted here, "The ConfigurationManager will read from the configuration file that was loaded by the AppDomain when it loaded the application". So in the case of your web application, it's going to read from the web.config file if you are accessing ConfigurationManager from within your aspx code.

I think the most suitable workaround to do what you want is the workaround I've already provided, namely, to use the appSettings configuration parameter which will allow you to have a shared configuration file for your aspx app and your class library.

As an example, in your shared configuration file, you'd want something like this:

<appSettings>
    <add key="Key1" value="test"/>
</appSettings>

Let's say this file was c:\temp\shared.config. Then in your web.config, you could refer to it like this:

<appSettings file="C:\\temp\\shared.config"></appSettings>

You would put this same line in your class library's app.config.

And now you'll be able to use:

ConfigurationManager.AppSettings["Key1"]

from either your aspx app or your class library and you'll get the value.

So if you really need "common" configuration parameters shared across aspx and class libraries, this is the way to do it.

like image 42
dcp Avatar answered Feb 09 '23 12:02

dcp