Unable to read App.config values ​​from class library

This may be a simple question to answer, but I'm struggling to solve.

I have a Web.config with the following values ​​in my web application.

 <appSettings> <add key ="FirstName" value ="Prasad"/> <add key ="LastName" value ="Kanaparthi"/> </appSettings> 

I have an App.config with the following value in my class library (Say Lib).

 <appSettings> <add key ="FullName" value ="Prasad Kanaparthi"/> </appSettings> 

The class library (Lib) is a pluggable component, I added the Class Library (Lib) link in my web application.

I have the code below in my class library (Lib). When I create an instance for GetAppSettings in my web application, I can see the answers below.

 namespace Lib { public class GetAppSettings { public GetAppSettings() { var FirstName = ConfigurationManager.AppSettings["FirstName"]; // O/P : Prasad var MiddleName = ConfigurationManager.AppSettings["LastName"]; // O/P : Kanaparthi var FullName = ConfigurationManager.AppSettings["FullName"]; // O/P : null } } } 

Question: how can I read FullName from App.config , which is a class library (Lib).

Note Since my Lib is a plugin, users can change their own FullName . I cannot combine values ​​in Web.confing with App.config .

+4
source share
3 answers

You need to combine both parts of the configuration and put all the settings in the main configuration file of your application. In the case of a web application, this will be web.config .

+5
source

Short answer: you cannot. An application using your library will not pay attention to your app.config . Instead, you can use the settings file, or perhaps go a longer way, abstract from the configuration manager and program its app.config as an XML file in the application’s output directory to enable its settings. It is clear that none of the options is perfect.

+3
source

Although I often don’t work with web applications, I often encounter a similar problem in my desktop application. I used the code provided by Daniel Hilgarth fooobar.com/questions/50096 / ... to temporarily switch configuration files. The bootloader plugin can insert the plugin configuration file and then restore it.

0
source

Source: https://habr.com/ru/post/1441766/


All Articles