PSV
PSV

Reputation: 31

Unable to make IDispatchMessageInspector work

I need to play with request header in a REST, Json, WCF web service. I create my IDispatchMessageInspector

public class HeaderInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        int ind = request.Headers.FindHeader("xxxxx", "");
        return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
    }
}

Then a endpointbehavio to attach the inspector to endpoints :

public class HeaderInspectorBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        HeaderInspector headerinsp = new HeaderInspector();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new HeaderInspector());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

And finally a BehaviorExtensionElement :

public class MyExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(HeaderInspectorBehavior); }
    }
    protected override object CreateBehavior()
    {
        return new HeaderInspectorBehavior();
    }
}

Those classes being in the same file / namespace PDM.WebService My config is :

<behaviors>
  <endpointBehaviors>
    <behavior name="RestBehavior">
      <HeaderInspectorBehavior/>
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="true" automaticFormatSelectionEnabled="false" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add name="HeaderInspectorBehavior" type="PDM.WebService.MyExtension, PDM.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

Everything compile well, at execution i can catch the execution of method "public override Type BehaviorType", but no other methods of the code are fired after that (i sent request and i got response, the service respond well). i set debug point in every methods nothing else execute ! (especially ApplyDispatchBehavior). Can somebody point out what i'm missing ?

Upvotes: 3

Views: 1227

Answers (1)

LCJ
LCJ

Reputation: 22661

Refer CreateBehavior() is not invoked for a similar issue.

Ensure that name of your service element is corresponding to mynamespace.myservicename

The service will be providing correct response even if you don't have the correct service name; but the CreateBehavior() will be called only when you have the correct name for the service element.

Example

<service
      name="WcfServiceApp001.Service1"
      behaviorConfiguration="InternalPayrollBehavior">
<endpoint address="" binding="basicHttpBinding"
          behaviorConfiguration="EndpointBehavior"
          contract="WcfServiceApp001.IService1"
          />
</service>

Upvotes: 0

Related Questions