I have a class library that has a function with this code:
this.apikey = ConfigurationManager.AppSettings["ApiKey"];
When I call this function in a console application, it says AppSettings has no keys. also it states the connection string is aspnet… whereas mine is sqlserver. So it is certainly not reading my library app config even though Im calling that in a library function.
Is this normal or am I doing something wrong?
I was hoping to avoid having to make and parse an xml and reinvent the wheel.
Thanks
It is normal and you are doing something wrong.
A library does not, usually, have its own configuration – it is subject to the configuration of the running application.
If you add your
appSettingsandconnectionStringsto the application configuration all will work.In other words, when an application loads and the libraries it uses are loaded, the application configuration is read – no other configuration file is read. When calling the static methods of
ConfigurationManager, the loaded configuration is what’s in effect.There are ways to load specific configuration files, but that’s not the default behaviour.