2

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

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.

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

Note: I am running the web service application from Visual Studio 2010.

Config

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

References

  1. http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6be701c0-25f9-4850-82f9-62a9b8e9ac04/
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

2

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.

Solution

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

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.

enter image description here

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>
LCJ
  • 22,196
  • 67
  • 260
  • 418
1

I suggest you to encapsulate your in behaviors node

 <behaviors>
  ....
  <endpointBehaviors>
    <behavior name="EndpointBehavior">
      <XMessageValidator validateRequest="True" validateReply="true" validateWSE="true">
      </XMessageValidator>
    </behavior>
  </endpointBehaviors>
  ....
 </behaviors>
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • It is already inside behaviors node. I just didn't put the complete config. Also I am able to reach up to ValidationBehaviorExtensionElement constructor – LCJ Mar 25 '13 at 12:25