Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect WCF Messages in the Callback direction?

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?

like image 755
Juan M. Elosegui Avatar asked Apr 25 '12 09:04

Juan M. Elosegui


1 Answers

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>
like image 81
Juan M. Elosegui Avatar answered Nov 16 '22 01:11

Juan M. Elosegui