我已经提到为什么找不到我的自定义 WCF 行为扩展元素类型?; 但以下是一个不同的问题
我有一个自定义的 BehaviorExtensionElement,如下所示。在运行服务时,它的构造函数被调用。但是它不调用 CreateBehavior() 方法。因此我的 IEndpointBehavior 没有被构建。
该服务工作正常,没有任何例外。
知道为什么CreateBehavior()
不调用该方法吗?
注意:我正在从Visual Studio 2010
.
配置
<endpointBehaviors>
<behavior name="EndpointBehavior">
<XMessageValidator validateRequest="True" validateReply="true" validateWSE="true">
</XMessageValidator>
</behavior>
</endpointBehaviors>
//Other config entries
<extensions>
<behaviorExtensions>
<add name="XMessageValidator" type="MessageInspectorLibrary.ValidationBehaviorExtensionElement, MessageInspectors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
行为扩展元素
public class ValidationBehaviorExtensionElement : BehaviorExtensionElement
{
public ValidationBehaviorExtensionElement()
{
//Constructor
}
public override Type BehaviorType
{
get
{
return typeof(MessageValidationBehavior);
}
}
protected override object CreateBehavior()
{
throw new Exception("My CreateBehavior");
return null;
}
[ConfigurationProperty("validateRequest", DefaultValue = false, IsRequired = false)]
public bool ValidateRequest
{
get { return (bool)base["validateRequest"]; }
set { base["validateRequest"] = value; }
}
[ConfigurationProperty("validateReply", DefaultValue = false, IsRequired = false)]
public bool ValidateReply
{
get { return (bool)base["validateReply"]; }
set { base["validateReply"] = value; }
}
[ConfigurationProperty("validateWSE", DefaultValue = false, IsRequired = false)]
public bool ValidateWSE
{
get { return (bool)base["validateWSE"]; }
set { base["validateWSE"] = value; }
}
}
端点行为
public class MessageValidationBehavior : IEndpointBehavior
{
XmlSchemaSet schemaSet;
bool validateRequest;
bool validateReply;
bool validateWSE;
public MessageValidationBehavior(XmlSchemaSet schemaSet, bool inspectRequest, bool inspectReply, bool inspectWSE)
{
this.schemaSet = schemaSet;
this.validateReply = inspectReply;
this.validateRequest = inspectRequest;
this.validateWSE = inspectWSE;
throw new Exception("My MessageValidationBehavior");
}
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
ValidationMessageInspector inspector = new ValidationMessageInspector(schemaSet, validateRequest, validateReply, validateWSE, true);
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
ValidationMessageInspector inspector = new ValidationMessageInspector(schemaSet, validateRequest, validateReply, validateWSE, false);
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
参考