Configuration.GetSection(“basicHttpBinding的”),不读从a

2019-10-17 00:39发布

我有以下特性:

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>

我想不通为什么它的失败。

Answer 1:

尝试这个:

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

<basicHttpBinding>不是配置部,它的内的元素<bindings>配置部分。



Answer 2:

看起来你得到了“basicHttpBinding的”部分,但随后试图将它转换为ServiceModelSectionGroup所以它返回它指的是“system.serviceModel”部分null

尝试ConfigurationManager.GetSection("system.serviceModel")来代替。



文章来源: Configuration.GetSection(“basicHttpBinding”) not reading that section from app.config file