自定义行为的配置错误 [英] Configuration Error With Custom Behaviour
问题描述
我创建了一个自定义行为,用于WCF服务将所有错误记录到应用程序日志中。我为行为创建了 BehaviorExtensionElement
:
I've created a custom behaviour for use with a WCF service to log all errors to the application log. I have made a BehaviorExtensionElement
for the behaviour:
public ErrorLoggingBehaviorExtensionElement : BehaviorExtensionElement
{
public ErrorLoggingBehaviorExtensionElement() { }
/* - Elements removed for brevity - */
}
我试图在我的配置中应用这个,如下:
I am attempting to apply this in my configuration as follows:
<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属性实际上在我的配置文件中的一行上,以克服 this known issue 。换行符已添加到您的眼睛。
NOTE: The "type" attribute on the behavior registration element is actually on a single line in my configuration file to overcome this known issue. Line-breaks have been added for your eyes.
尝试查看服务页面时会产生以下应用程序错误:
This generates the following application error when trying to view the service page:
为system.serviceModel / behavior创建配置节处理程序时出错:没有为此对象定义的无参数构造函数。
An error occurred creating the configuration section handler for system.serviceModel/behaviors: No parameterless constructor defined for this object.
删除< errorLogging />
元素会使错误消失,但我无法看到它与
Removing the <errorLogging />
element makes the error disappear, but I can't see how it is related to the error being reported.
推荐答案
问题实际上是在配置元素的子元素内。
The problem is actually deep within the configuration element's child elements.
其中一个配置属性是一个枚举,用 TypeConverterAttribute
装饰以执行从字符串到枚举的转换:
One of the configuration properties was an enumeration, decorated with a TypeConverterAttribute
to perform the conversion from string to enumeration:
[ConfigurationProperty("level", IsRequired=false)]
[TypeConverter(typeof(EnumConverter))]
public LogLevel Level
{
get { ... }
set { ... }
}
b $ b
抛出的异常实际上指的是类型 EnumConverter
不具有无参数构造函数(实际上它需要枚举的类型转换和从)
The exception being thrown actually refers to the type EnumConverter
not having a parameterless constructor (in fact it requires the type of the enumeration to convert to and from).
为了解决这个问题,我切换到在元素的构造函数中创建 ConfigurationProperty
,而不是使用声明模型。在某些时候,我可能会创建一个 EnumConverter< T>
类,以便它可以声明性地使用。
To resolve this, I switched to creating the ConfigurationProperty
in the element's constructor instead of using the declarative model. At some point, I will probably create an EnumConverter<T>
class so that it can be used declaratively.
这使我有一天的挖掘,最终解决。
This caused me about a day of digging to finally resolve.
这篇关于自定义行为的配置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!