子目录中的 web.config 中未选取 AppSettings 值
我们有一组 WCF 服务托管在 IIS 中。在我们的架构中,我们有不同的服务实例,它们的配置不同,托管在根目录下的子目录中。
我的根目录中有一个 Web.Config,它在 AppSettings 部分中有一些配置信息。具体来说,它看起来像这样:
<appSettings>
<add key="Environment" value="Local" />
</appSettings>
在不同的子目录中,我添加另一个 Web.config,它添加特定于该子目录中的服务的其他设置。
<appSettings>
<add key="Subdir" value="ABC" />
</appSettings>
但是,当我调用子目录中的服务 (.svc) 文件时,ConfigurationManager.AppSettings 集合中只有一个值(“Environment”)。找不到“Subdir”键。 (顺便说一句,代码是使用ConfigurationManager编写的,但我也尝试过使用WebConfigurationManager,结果是一样的。)
MSDN上的文档明确指出,嵌套目录中的Web.config文件应该是累积的。那么为什么我的“Subdir”键没有出现在该集合中?
预先感谢您能给我的任何帮助。
We have a set of WCF Services being hosted in IIS. In our architecture, we have different instances of the service that vary in configuration hosted in subdirectories under the root.
I have a Web.Config in the root that has some configuration information in the AppSettings section. Specifically, it looks like this:
<appSettings>
<add key="Environment" value="Local" />
</appSettings>
In the different subdirectories, I add another Web.config which adds other settings that are specific to the services in that subdirectory.
<appSettings>
<add key="Subdir" value="ABC" />
</appSettings>
However, when I invoke a service (.svc) file in the subdirectory, there is only one value in the ConfigurationManager.AppSettings collection ("Environment"). The "Subdir" key is nowhere to be found. (BTW, the code is written using ConfigurationManager, but I've also tried using WebConfigurationManager, and the result is the same.)
The documentation on MSDN clearly says that the Web.config files in nested directories are supposed to be cumulative. So why isn't my "Subdir" key showing up in that collection?
Thanks in advance for any help you can give me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了其他遇到此问题的人的利益,我找到了答案。
尽管此应用程序是在 IIS 下运行的服务,并且是通过 IIS 访问的,但需要此配置元素的点位于 System.ServiceModel.ServiceHostFactory 的派生类的深处。此时,HttpContext.Current 为 null,因此 ConfigurationManager 无法访问 Web 目录结构信息,而必须仅依赖于顶层的应用程序配置。
所以我们的解决方案是让每个子目录成为一个 IIS 应用程序,并在一个地方为其提供所有自己的配置信息。
For the benefit of anyone else that encounters this, I found the answer.
Even though this app is a service running under IIS, and is being accessed through IIS, the point where this config element is needed is deep down in a derived class of System.ServiceModel.ServiceHostFactory. At that point, HttpContext.Current is null, so the ConfigurationManager has no access to Web directory structure information, and has to depend simply on the application configuration, which is at the top level.
So our solution is to make each Subdirectory an IIS application, and give it all of its own config information in one place.