C# GetSection(“basicHttpBinding”)未从app.config文件中读取该节

C# GetSection(“basicHttpBinding”)未从app.config文件中读取该节,c#,configurationmanager,basichttpbinding,C#,Configurationmanager,Basichttpbinding,我拥有以下财产: protected BasicHttpBinding Binding { get { var config = ConfigurationManager.GetSection("basicHttpBinding") as ServiceModelSectionGroup; foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding.Bin

我拥有以下财产:

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;
    }
}           
当我调试它时,它会失败,出现一个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>

.....
....

我不明白它失败的原因。

看起来您得到的是“basicHttpBinding”部分,但尝试将其转换为
ServiceModelSectionGroup
,它引用了“system.serviceModel”部分,因此返回
null


请尝试使用
ConfigurationManager.GetSection(“system.serviceModel”)

看起来您得到的是“BasichtpBinding”部分,但随后尝试将其转换为
ServiceModelSectionGroup
,它引用了“system.serviceModel”部分,因此返回
null

请尝试
ConfigurationManager.GetSection(“system.serviceModel”)

尝试以下操作:

var config = ConfigurationManager.GetSection("system.serviceModel/bindings") as   
                        System.ServiceModel.Configuration.BindingsSection;
不是配置部分,它是
配置部分中的一个元素。

请尝试以下操作:

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

不是配置节,它是
配置节中的一个元素。

重复:。您应该只添加到您的问题中,而不是创建一个新问题。@Saied-我将其标记为删除。重复:。你应该只添加到你的问题中,而不是创建一个新的问题。@Saied-我标记它以将其删除。这是他在另一个问题中的内容。这是他在另一个问题中的内容。谢谢。这修复了我的配置为空的问题。不幸的是,它在我的foreach循环中打开了一整罐蠕虫。它不喜欢channelendpointelements,我尝试将其更改为的所有内容都会给我留下一个“foreach语句无法对变量进行操作…因为缺少公共GetEnumerator()。我会研究看看我能找到什么。谢谢。这解决了我的配置为空的问题。不幸的是,它在我的foreach循环中打开了一整罐蠕虫。它不喜欢ChannelEndpoint元素,我尝试并更改它的所有内容都给我留下了一个“由于缺少公共GetEnumerator(),foreach语句无法对变量进行操作。我会研究一下,看看能找到什么。