简体   繁体   English

用户定义的WCF绑定配置中无法识别的属性名称

[英]Unrecognized Attribute Name in User-Defined WCF Binding Configuration

I'm writing a custom WCF binding that would like to control using configuration. 我正在编写一个自定义WCF绑定,该绑定想控制使用配置。 To be clear, I'm not talking about using the standard <customBinding> element, but rather inheriting from Binding and writing a full class. 明确地说,我不是在谈论使用标准的<customBinding>元素,而是从Binding继承并编写完整的类。

My binding isn't very complicated, and I really only have one property that I would like to set through config, useSsl . 我的绑定不是很复杂,实际上我只有一个要通过config设置的属性useSsl However, I am having difficulty getting .NET to recognize my configuration attribute even though I believe I have everything in order. 但是,即使我相信一切都井井有条,我仍然很难使.NET识别我的配置属性。

Binding 捆绑

public class MyCustomBinding : Binding {

    public MyCustomBinding() {
        // Initialization
    }

    private bool _useSsl;

    public bool UseSsl {
        get { return _useSsl; }
        set { _useSsl = value; }
    }

    // Remaining implementation omitted

}

Binding Configuration Element 绑定配置元素

public class MyCustomBindingElement : StandardBindingElement {

      protected override Type BindingElementType {
          return typeof(MyCustomBinding);
      }

      // public const string UseSsl = "useSsl"
      [ConfigurationProperty(ConfigurationStrings.UseSsl, DefaultValue: true)]
      public bool UseSsl {
          get { return (bool)this[ConfigurationStrings.UseSsl]; }
          set { this[ConfigurationStrings.UseSsl] = value; }
      }


      // Remaining implementation omitted

}

Binding Configuration Collection Element 绑定配置收集元素

public class MyCustomBindingCollectionElement
    : StandardBindingCollectionElement<MyCustomBinding, MyCustomBindingElement> {}

I've registered the collection element in my web.config file: 我已经在我的web.config文件中注册了collection元素:

  <extensions>
      <bindingExtensions>
          <add name="myCustomBinding" type="MyCustomWcf.MyCustomBindingCollectionElement, MyCustomWcf"/>
      </bindingExtensions>
  </extensions>

Then in my <bindings> section I add an instance of my custom binding. 然后在我的<bindings>部分中,添加一个自定义绑定的实例。

<myCustomBinding>
    <binding useSsl="false" />
</myCustomBinding>

However, I receive the following configuration exception at runtime: 但是,我在运行时收到以下配置异常:

Unrecognized attribute 'useSsl'. 无法识别的属性“ useSsl”。 Note that attribute names are case-sensitive 请注意,属性名称区分大小写

If I do not specify any properties on my binding, then it works. 如果我没有在绑定上指定任何属性,那么它将起作用。 What am I doing wrong here? 我在这里做错了什么?

At a quick glance. 一目了然。 You specify ConfigurationStrings.UseSsl, but in the config you put useSsl. 您指定ConfigurationStrings.UseSsl,但在配置中放置useSsl。 Can you correct this and try again? 您可以更正此问题,然后重试吗?

The "Properties" property of MyCustomBindingElement also need to be updated as below. MyCustomBindingElement的“属性”属性也需要进行如下更新。 Hope helpful ... 希望对您有帮助...

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            var result = base.Properties;
            result.Add(new ConfigurationProperty("useSsl", typeof(bool)));

            return base.Properties;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM