繁体   English   中英

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

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

我正在编写一个自定义WCF绑定,该绑定想控制使用配置。 明确地说,我不是在谈论使用标准的<customBinding>元素,而是从Binding继承并编写完整的类。

我的绑定不是很复杂,实际上我只有一个要通过config设置的属性useSsl 但是,即使我相信一切都井井有条,我仍然很难使.NET识别我的配置属性。

捆绑

public class MyCustomBinding : Binding {

    public MyCustomBinding() {
        // Initialization
    }

    private bool _useSsl;

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

    // Remaining implementation omitted

}

绑定配置元素

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

}

绑定配置收集元素

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

我已经在我的web.config文件中注册了collection元素:

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

然后在我的<bindings>部分中,添加一个自定义绑定的实例。

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

但是,我在运行时收到以下配置异常:

无法识别的属性“ useSsl”。 请注意,属性名称区分大小写

如果我没有在绑定上指定任何属性,那么它将起作用。 我在这里做错了什么?

一目了然。 您指定ConfigurationStrings.UseSsl,但在配置中放置useSsl。 您可以更正此问题,然后重试吗?

MyCustomBindingElement的“属性”属性也需要进行如下更新。 希望对您有帮助...

    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