问题
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