O.O
O.O

Reputation: 11317

Why app.config value null?

I have a three tier web app that produces three separate dlls:

Both Web.Application.dll and Web.DAL.dll have their own app.configs. I need to access a specific setting that lives in web.config. I am using the following code:

[CacheUtil.cs]
string cacheName = ConfigurationManager.AppSettings.Get("CacheName");

I have verified that this setting exists. So, why then, when I run am I getting NULL? Does CacheName need to exist in all of .config files?

Here is the setting in web.config:

  <appSettings>
    <add key="CacheName" value="staging"/>

FYI, CacheUtil is a singleton that is lazy initialized upon first access. First access is happening in the DAL.dll project.

Thanks!

Upvotes: 2

Views: 636

Answers (2)

Andrew Barber
Andrew Barber

Reputation: 40160

Only web.config is being read. The other config files (Web.Application.dll.config and Web.DAL.config) are not read.

Config is not attached to libraries; only to the ultimate executable being run (the AppName.exe.config or web.config)

Projects in Visual Studio that produce an EXE can have an app.config file, and this file is treated specially upon build; It is renamed to ProgramName.exe.config and copied to the output directory. app.config in class library or Web application projects will have no effect.

Upvotes: 4

user596075
user596075

Reputation:

Try this:

cachename = ConfigurationManager.AppSettings["CacheName"];

This is provided that you are trying to read AppSettings out of the project's app.config file. You can't read config from another project unless you use some other logic, but it wouldn't be through the ConfigurationManager class. It'd have to be normal I/O.

Upvotes: 1

Related Questions