Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable read App.config values from Class Library

Tags:

c#

asp.net

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.

like image 866
Prasad Kanaparthi Avatar asked Oct 24 '12 09:10

Prasad Kanaparthi


2 Answers

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.

like image 124
Wiktor Zychla Avatar answered Oct 21 '22 22:10

Wiktor Zychla


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.

like image 23
Steve Wilkes Avatar answered Oct 21 '22 23:10

Steve Wilkes