Why app.config value null?
I have a three tier web app that produces three separate dlls:
- Web.UI.dll
- Web.Application.dll
- Web.DAL.dll
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 = C开发者_运维问答onfigurationManager.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!
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.
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.
精彩评论