5

This may be the simple question to answer, but I am struggling to resolve.

I have Web.config with the following values in my Web Application.

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

I have App.config with following value in my Class Library (Say Lib).

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

The class library(Lib) is pluggable component, I have added the Class Library(Lib) reference in my Web Application.

I have the below code in my Class Library(Lib). When i create instance for GetAppSettings in my web application i can see below responses.

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

The Question is how can i read FullName from App.config which is Class Library (Lib).

Note: Since my Lib is pluggable component, So the consumers can change the FullName of their own. I cannot merge values to Web.confing from App.config.

Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62

3 Answers3

6

You have to merge both configuration sections and place all settings in the main configuration file of your application. In case of the web applciation it would be the web.config.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • I can't merge both (Web.confing & App.config). Because the Class Library is pluggable component. – Prasad Kanaparthi Oct 24 '12 at 09:19
  • You have to. You plug your dll but you merge your settings. It is possible to to have separate configuration files but this requires extra code to read these separate files. – Wiktor Zychla Oct 24 '12 at 09:31
  • Similar situation with `connectionString`. I had to remove it from class library `App.config` and add it to the `web.config` of my main project. +1 – Kaf Dec 12 '13 at 18:06
3

The short answer is: you can't. The application using your library won't pay any attention to your app.config. You could use a settings file instead or perhaps go a longer way around, abstract out the configuration manager and have it programmatically read your app.config as an XML file in the application's output directory in order to include its settings. Understandably, neither option is ideal.

Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
0

While I don't work often with web applications, in my desktop application I often face a similar issue. I've been using the code provided by Daniel Hilgarth https://stackoverflow.com/a/6151688/2212458 to temporarily switch configuration files. The plugin loader can pull in the plugin configuration file and then restore its own.

Community
  • 1
  • 1
denver
  • 2,863
  • 2
  • 31
  • 45