Question

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

Was it helpful?

Solution

Is this normal or am I doing something wrong?

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 appSettings and connectionStrings to 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.

OTHER TIPS

You are certainly wrong at some place. May be you have written your keys in different section than the appSettings. Just check it, and if it is correct then , you will need to reinvent the wheel as below:

XDocument.Load(HttpContext.Current.Server.MapPath("~/web.config"));

Be sure to add System.Web namespace to your project before using HttpContext

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top