未调用CreateBehavior() [英] CreateBehavior() is not invoked

查看:56
本文介绍了未调用CreateBehavior()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我推荐了​​为什么没有找到我的自定义WCF行为扩展元素类型?;但以下是一个不同的问题

I have referred Why isn't my custom WCF behavior extension element type being found? ; but the following is a different question

我有一个自定义的BehaviorExtensionElement,如下所示.在运行服务时,将调用其构造函数.但是,它不会调用CreateBehavior()方法.因此,我的IEndpointBehavior无法构建.

I have a custom BehaviorExtensionElement as shown below. While running the service, it’s constructor is getting called. However it does no call the CreateBehavior() method. Hence my IEndpointBehavior is not getting constructed.

该服务正常运行,没有任何异常.

The service works fine without any exception.

是否知道为什么未调用 CreateBehavior()方法?

Any idea why the CreateBehavior() method is not invoked?

注意:我正在从 Visual Studio 2010 运行Web服务应用程序.

Note: I am running the web service application from 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>

BehaviorExtensionElement

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; }
    }

}

IEndpointBehavior

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
}

参考

  1. >://social.msdn.microsoft.com/Forums/zh-CN/wcf/thread/6be701c0-25f9-4850-82f9-62a9b8e9ac04/

推荐答案

注意:正如我在问题中所说,即使没有以下更改,该服务也会给出正确的响应消息.另外,ValidationBehaviorExtensionElement类也被调用.

Note: As I said in the question, the service gives proper response message even without the following change. Also, the ValidationBehaviorExtensionElement class was getting called.

解决方案

当我使服务名称正确时,即 namespace.servicename ,将调用 CreateBehavior().

The CreateBehavior() is called when I made the service name correct – i.e, namespace.servicename.

我了解的是-无论服务名称如何,都会创建 BehaviorExtension .但是,只有在服务名称正确的情况下,才会创建 EndPointBehavior .如果您对此有任何想法/参考,欢迎提供更多详细信息.

What I understand is - BehaviorExtension is created irrespective of the service name. But EndPointBehavior is created only if the service name is proper. More details are welcome if you have some idea/reference on this.

以下是完整的serviceModel配置

Following is the complete serviceModel config

<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>

这篇关于未调用CreateBehavior()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆