-->

如何阅读basicHttpBinding的在app.config文件中定义(How to read

2019-09-19 09:32发布

我试图找出如何阅读如何阅读的app.config文件中定义basicHttpBinding的部分。 我试图做到这一点跟我一样,端点地址,但它不工作。 这里是我做了我的端点地址:

private EndpointAddress EndPointAddy
{
    get
    {
        var config = ConfigurationManager.GetSection("system.serviceModel/Client") as ServiceModelSectionGroup;                
        foreach (ChannelEndpointElement endpoint in config.Client.Endpoints)
        {
            Uri address = endpoint.Address;

            if (address != null)
            {
                return new EndpointAddress(address);                         
            }          
        }
        return null;           
    }
}  

下面是我用basicHttpBinding的尝试

private BasicHttpBinding Binding
{
    get
    {
        var config = ConfigurationManager.GetSection("system.serviceModel/Client") as ServiceModelSectionGroup;
        foreach (ChannelEndpointElement bindings in config.Bindings.BasicHttpBinding)
        {
            string binding = config.Bindings.BasicHttpBinding;

            if (config.Bindings.BasicHttpBinding != null)
            {
                return new BasicHttpBinding(config.Bindings.BasicHttpBinding);
            }
        }
        return null;
    }

但是我收到的是一条错误:

“foreach语句无法在类型‘System.ServiceModel.Configuration.BasicHttpBindingCollectionElement’的变量操作,因为‘System.ServiceModel.Configuration.BasicHttpBindingCollectionElement’不包含‘的GetEnumerator’一个公共定义”

我试着BasicHttpBindingElement更换ChannelEndpointElement但这并不能帮助。 我不知所措,使我得到的的EndpointAddress同样的效果如何正确地做到这一点。

编辑:我想我有这个固定的,但我所做的就是摆脱错误的。 当我尝试调试程序时,我收到了

System.NullReferenceException:对象未设置为一个对象的一个​​实例

在以下点:

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

然而,即使它无法在这一行,我注意到,它上面的一行:

var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup;

也为NULL。 我不知道它是什么,我做错了。

文章来源: How to read basicHttpBinding defined in app.config file