1

In .NET solutions, we can read settings from configuration files, such as web.config or app.config. But I found a problem while using these resources. In a solution, I created a Class Library Project and an ASP.NET Project, both of them have a "something.config" file. In Class Library, it is called App.Config, and in ASP.NET, obviously, Web.config.

I created a method in a class in the Class Library Project and invoked it in ASP.NET. These methods gets some settings from app.config, but if I try to use in my Web project, the method read from web.config, in ASP.NET project, what is not desirable.

How can I do to make the method in Class Library Project read the App.Config from corresponding project, not from web project?

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
Matheus Gelinski
  • 438
  • 1
  • 5
  • 15
  • possible duplicate of http://stackoverflow.com/questions/690313/using-app-config-with-a-class-library – Dave Becker Feb 03 '15 at 12:10
  • 1
    In general a library has no business accessing config files. The application should pass the configuration to the libraries it uses. – CodesInChaos Feb 03 '15 at 12:31
  • Have you tried anything yet? If so then show some code the way you are trying to do it.. – Suhaib Janjua Feb 03 '15 at 12:37
  • But in cases when a class library application cannot always depends on target application, it can generate a problem in souce application. My idea is to prevent my classes to make errors because client publishing failures (i.e. forgetting keys or wrong values). – Matheus Gelinski Feb 03 '15 at 12:39
  • @Joker There has no secrets, it is just using the ConfigurationManager.AppSettings["settingname"]. But if we use it in class library project, it will read from web.config on web project, not from app.config. – Matheus Gelinski Feb 03 '15 at 12:40

2 Answers2

1

It depends on the application you are running.

The web application is the entry point hence it'll use the web.config.

You can put the values/sections contained in the app.config into the web.config and it'll be read.

After digging out the configuration manager class i've added the below to read config file associated with an assembly.

var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

var tempValue = config.AppSettings["setting_name"];
scartag
  • 17,548
  • 3
  • 48
  • 52
  • Yeah, I know. But I wanted to avoid to do it. I didn't want to depend on the person who will publish the web project to put in the web.config file in web application, while I have the same settings in class library project. I wanted to make the method always read the app.config in the source project, where it has been written. – Matheus Gelinski Feb 03 '15 at 12:03
  • @MatheusGelinski i've modified my answer to include an option – scartag Feb 03 '15 at 12:09
  • It can be a stupid question, but what is this "Assembly_name" (or type)? – Matheus Gelinski Feb 03 '15 at 12:21
  • @MatheusGelinski refresh the page ... i've modified the answer because i realize you'll be accessing the config values from the dll in question so the best option would be to use Assembly.GetExecutingAssembly(). – scartag Feb 03 '15 at 12:27
  • I can't execute the last line, but I changed to: var settings = config.GetSection("appSettings") as AppSettingsSection; var tempValue = settings.Settings["test"].Value; But you answer helped me a lot! – Matheus Gelinski Feb 03 '15 at 13:01
0

App.config that places in the class library project wouldn't load by default. The only config file will be loaded is the client configuration (web.config).

To configure the project to load the local class library config file which is app.config.

You need the following code to do that:

ConfigurationManager.OpenMappedExeConfiguration(
    new ExeConfigurationFileMap() { 
        ExeConfigFilename = path + "app.config" 
    }, ConfigurationUserLevel.None);

For more details, visit this answer.

Community
  • 1
  • 1
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73