Using ConfigurationManager.GetSection in a Class Library's App.config

▼魔方 西西 提交于 2019-12-31 03:09:26

问题


I am writing a class library which has settings in its app.config and which will ultimately be called by a small number of other .NET applications. In order to get settings from it I'm using ConfigurationManager.GetSection such as this:

MyConfiguration process = (MyConfiguration)ConfigurationManager.GetSection("MyGroup/processes");

I've discovered though that the calling application has to have the same app.config inside it's own project in order for this to work, otherwise the class library will throw a NullReferenceException. I'm just wondering if this is normal behavior or if there's any way to ensure that only the class library needs to have app.config available?

Thanks :)


回答1:


Your class library will always attempt to read from the app.config of the main application that references it. It will not use your class library config file at all.



来源:https://stackoverflow.com/questions/13141003/using-configurationmanager-getsection-in-a-class-librarys-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!