我正在为 WCF 添加自定义行为扩展元素,并希望添加一个可以在读取配置元素时读取的属性,例如
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="myExtension"
type="Bar.FooBarElement, Bar"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<myExtension myAttribute="Foo" />
但是,我收到错误消息“无法识别的属性 'myAttribute'。请注意,属性名称区分大小写。”
我怎样才能避免这种情况?如何读取代码中的 myAttribute 值?
最佳答案
事实证明这很容易,因为 BehaviorExtensionElement 是 ConfigurationElement 的子类,所以通常的配置规则适用。
[ConfigurationProperty("myAttribute")]
public string MyAttribute
{
get { return (string)this["myAttribute"]; }
set { this["myAttribute"] = value; }
}
关于wcf - 向 BehaviorExtensionElement 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7154614/