- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已转介 Why isn't my custom WCF behavior extension element type being found? ;但以下是一个不同的问题
我有一个自定义的 BehaviorExtensionElement,如下所示。在运行服务时,它的构造函数被调用。但是它不会调用 CreateBehavior() 方法。因此我的 IEndpointBehavior 没有被构建。
该服务工作正常,无一异常(exception)。
知道为什么 CreateBehavior()
方法没有被调用?
注意:我正在运行来自 Visual Studio 2010
的 Web 服务应用程序.
配置
<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
}
最佳答案
注意:正如我在问题中所说,即使没有以下更改,该服务也会提供正确的响应消息。此外,ValidationBehaviorExtensionElement 类被调用。
解决方案 CreateBehavior()
当我使服务名称正确时调用 - 即 namespace.servicename
.
我的理解是- BehaviorExtension
无论服务名称如何,都会创建。但是EndPointBehavior
仅当服务名称正确时才创建。如果您对此有一些想法/引用,欢迎提供更多详细信息。
以下是完整的 serviceModel 配置
<system.serviceModel>
<services>
<service
name="WcfServiceApp001.Service1"
behaviorConfiguration="InternalPayrollBehavior">
<endpoint address="" binding="basicHttpBinding"
behaviorConfiguration="EndpointBehavior"
contract="WcfServiceApp001.IService1"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="InternalPayrollBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpointBehavior">
<XMessageValidator validateRequest="True" validateReply="true" validateWSE="true">
</XMessageValidator>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="XMessageValidator" type="MessageInspectorLibrary.ValidationBehaviorExtensionElement, MessageInspectors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
关于c# - 未调用 CreateBehavior(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610186/
我已转介 Why isn't my custom WCF behavior extension element type being found? ;但以下是一个不同的问题 我有一个自定义的 Beha
我是一名优秀的程序员,十分优秀!