我创建了一个与 WCF 服务一起使用的自定义行为,以将所有错误记录到应用程序日志中。我做了一个BehaviorExtensionElement
对于行为:
public ErrorLoggingBehaviorExtensionElement : BehaviorExtensionElement
{
public ErrorLoggingBehaviorExtensionElement() { }
/* - Elements removed for brevity - */
}
我尝试将其应用到我的配置中,如下所示:
<extensions>
<behaviorExtensions>
<add name="errorLogging"
type="ErrorLoggingBehaviorExtensionElement, Logging, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=56e8273d901d717f"/>
</behaviorExtensions>
</extensions>
<services>
<service name="TestService" behaviorConfiguration="TestServiceBehavior">
<endpoint address=""
binding="wsHttpBinding"
contract="Test_Service.ITestService"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<errorLogging />
</behavior>
</serviceBehaviors>
</behaviors>
注意:行为注册元素上的“type”属性实际上位于我的配置文件中的一行上,以克服这个已知问题 http://connect.microsoft.com/wcf/feedback/details/216431/wcf-fails-to-find-custom-behaviorextensionelement-if-type-attribute-doesnt-match-exactly。为您的眼睛添加了换行符。
尝试查看服务页面时,这会生成以下应用程序错误:
为 system.serviceModel/behaviors 创建配置节处理程序时发生错误:没有为此对象定义无参数构造函数。
删除<errorLogging />
元素使错误消失,但我看不出它与报告的错误有什么关系。
问题实际上出在配置元素的子元素的深处。
配置属性之一是枚举,用TypeConverterAttribute
执行从字符串到枚举的转换:
[ConfigurationProperty("level", IsRequired=false)]
[TypeConverter(typeof(EnumConverter))]
public LogLevel Level
{
get { ... }
set { ... }
}
抛出的异常实际上指的是类型EnumConverter
没有无参数构造函数(实际上它需要枚举类型进行相互转换)。
为了解决这个问题,我转而创建ConfigurationProperty
在元素的构造函数中而不是使用声明性模型。在某个时候,我可能会创建一个EnumConverter<T>
类,以便可以声明性地使用它。
这让我花了一天的时间终于解决了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)