1

我有以下财产:

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>

我不知道为什么它失败了。

4

2 回答 2

4

试试这个:

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

<basicHttpBinding>不是配置部分,它是配置部分中的一个元素<bindings>

于 2012-07-12T20:41:27.433 回答
3

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

试试ConfigurationManager.GetSection("system.serviceModel")吧。

于 2012-07-12T20:26:21.287 回答