1

I am trying to read settings in a class file, from my app.config file. Both files are in a ClassLibrary. Folder structure is as follows:

ClassLibrary
-Folder1
--Folder2
---ClassFile.cs

App.config

The values returned are Null. Kindly advise.

Vipul
  • 107
  • 2
  • 4
  • 13
  • Do you have a solution with two projects? (one of them the ClassLibrary) – Alejandro Martin Sep 10 '11 at 09:11
  • @Alejandro Martin: Yes I do have. Actually, I am calling a method in class file which is in a classlibrary(business tier). The method reads certain values from app.config. This method is invoking another method in data tier which performs certain operations. – Vipul Sep 10 '11 at 10:07
  • 1
    Then your application is looking in the config file at the project set as default (not the one in the classLibrary project). – Alejandro Martin Sep 10 '11 at 11:28
  • @Alejandro Martin: then how can I make it read it from the ClassLibrary ? – Vipul Sep 10 '11 at 14:51
  • 2
    As @Oded stated, the app.config and the web.config files are configuration files for applications, not libraries. You should expect to find the configuration settings at the app.config of the application project. If you really need to read configuration values from a file in your library project, you always can use a XML file (and include this file in the output of your project), but this is a bad design. – Alejandro Martin Sep 10 '11 at 15:11

1 Answers1

2

Class libraries do not have configuration files - applications do.

One reason is that class libraries cannot be directly executed - they can only be run in the context of an application.

Have the app.config file in your application/test harness (exe or website, for example). The library will be able to read the configuration at that point.

Note: You should be passing configuration into your library as a dependency. This is better design - the application can tell the library how to be configured and there is the added bonus of testability (you can test without having to have a .config).

See this SO answer for more details.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009