代码之家  ›  专栏  ›  技术社区  ›  LCJ

未调用CreateBehavior()

  •  2
  • LCJ  · 技术社区  · 13 年前

    我已经提到 Why isn't my custom WCF behavior extension element type being found? ; 但以下是一个不同的问题

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

    服务运行良好,无一例外。

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

    注意:我正在从运行web服务应用程序 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>
    

    BehaviorExtension元素

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

    E端点行为

    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. http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6be701c0-25f9-4850-82f9-62a9b8e9ac04/
    2 回复  |  直到 8 年前
        1
  •  2
  •   LCJ    13 年前

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

    解决方案

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

    我所理解的是- BehaviorExtension 无论服务名称如何,都会创建。但是 EndPointBehavior 只有在服务名称正确的情况下才会创建。如果您对此有一些想法/参考,欢迎提供更多细节。

    enter image description here

    以下是完整的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>
    
        2
  •  1
  •   Aghilas Yakoub    13 年前

    我建议你把你的 behaviors node

     <behaviors>
      ....
      <endpointBehaviors>
        <behavior name="EndpointBehavior">
          <XMessageValidator validateRequest="True" validateReply="true" validateWSE="true">
          </XMessageValidator>
        </behavior>
      </endpointBehaviors>
      ....
     </behaviors>