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

本文关键字:识别 属性 定义 WCF 绑定 配置 用户 | 更新日期: 2023-09-27 18:10:55

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

我的绑定不是很复杂,我真的只有一个属性,我想通过配置设置,useSsl。然而,我很难让。net识别我的配置属性,尽管我相信我已经把一切都安排妥当了。

<<p> 绑定/strong>
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中注册了集合元素。配置文件:

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

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

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

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

无法识别的属性'useSsl'。注意属性名称区分大小写

如果我没有在绑定上指定任何属性,那么它就可以工作。我哪里做错了?

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

快速浏览。您指定configurationstring。UseSsl,但是在配置中你把UseSsl。你能纠正这个并再试一次吗?

MyCustomBindingElement的"Properties"属性也需要更新如下:希望对你有帮助。

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