【问题标题】:Configuration.GetSection("basicHttpBinding") not reading that section from app.config fileConfiguration.GetSection("basicHttpBinding") 没有从 app.config 文件中读取该部分
【发布时间】:2012-07-12 16:20:30
【问题描述】:

我有以下属性:

protected BasicHttpBinding Binding
{
    get
    {
        var config = ConfigurationManager.GetSection("basicHttpBinding") as ServiceModelSectionGroup;
        foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bindings)
        {
            string binding = bindings.Binding;

            if (binding != null)
            {
                return new BasicHttpBinding(binding);
            }
        }
        return null;
    }
}           

当我调试它时,它失败并出现空异常:对象引用未设置为该行的对象实例:

foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bindings)

但是,我注意到它上面的行也是空的。

这里是 app.config 文件

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="IntelexWSSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>

.....

            </basicHttpBinding>
        </bindings>
    ....
    </system.serviceModel>
</configuration>

我不知道为什么会失败。

【问题讨论】:

标签: c# configurationmanager basichttpbinding


【解决方案1】:

看起来您正在获取“basicHttpBinding”部分,但随后尝试将其转换为引用“system.serviceModel”部分的ServiceModelSectionGroup,因此它返回null

改用ConfigurationManager.GetSection("system.serviceModel")

【讨论】:

  • 这是他在另一个问题中的内容。
【解决方案2】:

试试这个:

var config = ConfigurationManager.GetSection("system.serviceModel/bindings") as   
                        System.ServiceModel.Configuration.BindingsSection;

&lt;basicHttpBinding&gt; 不是配置部分,它是&lt;bindings&gt; 配置部分中的一个元素。

【讨论】:

  • 谢谢。这解决了我的配置为空的问题。不幸的是,它在我的 foreach 循环中打开了另一罐蠕虫。它不喜欢 channelendpointelements 以及我尝试将其更改为的所有内容都给我留下了“foreach 语句无法对变量进行操作...由于缺少公共 GetEnumerator()。我会研究并看看我能弄清楚什么。
猜你喜欢
  • 2011-01-24
  • 2010-10-11
  • 2012-06-26
  • 2023-03-21
  • 2018-07-27
  • 2013-08-12
  • 2015-03-22
  • 2021-07-08
  • 2012-03-29
相关资源
最近更新 更多