无法使用ConfigurationManager.AppSettings读取Web.config [英] Can't read Web.config with ConfigurationManager.AppSettings

查看:88
本文介绍了无法使用ConfigurationManager.AppSettings读取Web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个WCF服务,该服务使用Web.config来获取一些appSettings.在Visual Studio中,它运作良好,但是当我发布并安装该服务时,它突然从App.config中而不是从Web.config中获取其appSettings.

I have build a WCF service that uses Web.config to get some appSettings. In visual studio it works great but when I publish and instal the service it suddenly gets its appSettings from App.config and not from Web.config.

之所以知道这一点,是因为我遍历appSettings并使用以下代码将结果打印到控制台:

I know this because I loop through appSettings and printed the result to the console with this code:

foreach (string key in ConfigurationManager.AppSettings.AllKeys)
{
     Console.WriteLine("Name: {0} Value: {1}", key, ConfigurationManager.AppSettings[key]);
}

我的配置如下:

Web.config

  ....
  <appSettings>
    <add key="IQDir" value="C:\Program Files (x86)\Ridder iQ Client (lokaal)\Bin"/>
    <add key="FileURL" value="localhost:8080/WebPortal_2.0/"/>
  </appSettings>
  ....

App.config

....
  <appSettings>
    <add key="test1" value="wtf is going on!"/>
    <add key="test2" value="waargh"/>
    <add key="test3" value="I am getting a headache over here!!"/>
  </appSettings>
....

当我在Visual Studio中运行时,我得到:

When I run in visual studio I get:

但是当我在实时环境中使用发布的代码时,我得到了:

But when I use the published code inside live environment I get this:

为什么会发生这种情况,以及如何强制 ConfigurationManager 从Web.config而不是App.config获取appSettings.

Why is this happening and how can I force ConfigurationManager to get appSettings from Web.config instead of App.config.

推荐答案

如果您有标准的WCF项目,则应仅具有Web.config文件,而不应具有App.config.

If you have a standard WCF-project, you should only have the Web.config-file, not App.config.

我将完全跳过使用appSettings的旧方法.通过使用项目属性中的设置"标签,可以改用applicationSettings.

I would skip the old way of using appSettings altogether. Use applicationSettings instead, by using the Settings-tab in the project's properties.

它将在Web.Config中创建它:

It will create this in Web.Config:

<applicationSettings>
    <Projectname.Properties.Settings>
        <setting name="Testenvironment" serializeAs="String">
            <value>True</value>
        </setting>
    </Projectname.Properties.Settings>
</applicationSettings>

有关更多信息: appSettings vs applicationSettings.appSettings过时了吗?

这篇关于无法使用ConfigurationManager.AppSettings读取Web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆