8

I can inspect WCF messsages on both Client side and server side using IClientMessageInspector, IDispatchMessageInspector respectively. But in a Duplex comunications it is not clear how to do it in a callback from server to client (Nor much documentation on that topic).

Any ideas about how to implement this feature?

1
  • I am also facing the same issue.
    – Anuraj
    Commented Apr 26, 2012 at 9:58

1 Answer 1

7

Finally I get the solution.

In a Duplex comunication scenario when a callback is made the server becomes the client and vice versa.

So on server side when implementing IServiceBehavior inject the message inspector using the CallbackClientRuntime property of the DispatchRuntime foreach EndpointDispatcher.

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
    foreach (ChannelDispatcher item in serviceHostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher epd in item.Endpoints)
        {
            //injecting an inspector in normal call
            epd.DispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());

            //injecting an inspector in callback
            epd.DispatchRuntime.CallbackClientRuntime.MessageInspectors.Add(new MessageSizerInspector());
        }
    }
}

On client side when implementing IEndpointBehavior inject the message inspector using the CallbackDispatchRuntime.

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
    //injecting an inspector in normal call
    clientRuntime.MessageInspectors.Add(new MessageSizerInspector());

    //injecting an inspector in callback
    clientRuntime.CallbackDispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());       
}

Then apply the extension as always.

In my case I created a class like the following pseudo code

public class MessageSizer : Attribute, IServiceBehavior, IEndpointBehavior
{
    .....
}

then I applied this attribute to service implementation for the server side inspection and added a behaviorExtensions inside the app.config to setup the endpoint for message inspection on client side.

<system.serviceModel>
    ...........
    <client>
      <endpoint address="net.tcp://localhost/MinerDual.svc"
            binding="netTcpBinding" bindingConfiguration="wsDualMinerNetTcp"
            contract="WebApplication.IMinerDual" name="NetTcpMinerDual" 
            behaviorConfiguration="Default" />
    </client>
  <behaviors>
    <endpointBehaviors >
      <behavior name="Default">
        <messageSizer/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <extensions>
    <behaviorExtensions>
      <add name="messageSizer"
           type="WCFExtensions.MessageSizerElement, WCFExtensions, 
           Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
</system.serviceModel>

Not the answer you're looking for? Browse other questions tagged or ask your own question.