Cannot read setting from app.config file in same classlibrary
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. Kin开发者_如何学Cdly advise.
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.
精彩评论