How to: Inspect or Modify Messages on the Client

You can inspect or modify the incoming or outgoing messages across a WCF client by implementing a System.ServiceModel.Dispatcher.IClientMessageInspector and inserting it into the client runtime. For more information, see Extending Clients. The equivalent feature on the service is the System.ServiceModel.Dispatcher.IDispatchMessageInspector. For a complete code example see the Message Inspectors sample.

To inspect or modify messages

  1. Implement the System.ServiceModel.Dispatcher.IClientMessageInspector interface.

  2. Implement a System.ServiceModel.Description.IEndpointBehavior or System.ServiceModel.Description.IContractBehavior depending upon the scope at which you want to insert the client message inspector. System.ServiceModel.Description.IEndpointBehaviorallows you to change behavior at the endpoint level. System.ServiceModel.Description.IContractBehaviorallows you to change behavior at the contract level.

  3. Insert the behavior prior to calling the System.ServiceModel.ClientBase<TChannel>.Open or the System.ServiceModel.ICommunicationObject.Open method on the System.ServiceModel.ChannelFactory<TChannel>. For details, see Configuring and Extending the Runtime with Behaviors.


The following code examples show, in order:

  • A client inspector implementation.

  • An endpoint behavior that inserts the inspector.

  • A BehaviorExtensionElement- derived class that allows you to add the behavior in a configuration file.

  • A configuration file that adds the endpoint behavior which inserts the client message inspector into the client runtime.

// Client message inspector  
public class SimpleMessageInspector : IClientMessageInspector  
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)  
        // Implement this method to inspect/modify messages after a message  
        // is received but prior to passing it back to the client   
        Console.WriteLine("AfterReceiveReply called");  

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)  
        // Implement this method to inspect/modify messages before they   
        // are sent to the service  
        Console.WriteLine("BeforeSendRequest called");  
        return null;  
// Endpoint behavior  
public class SimpleEndpointBehavior : IEndpointBehavior  
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)  
         // No implementation necessary  

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)  
        clientRuntime.MessageInspectors.Add(new SimpleMessageInspector());  

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)  
         // No implementation necessary  

    public void Validate(ServiceEndpoint endpoint)  
         // No implementation necessary  
// Configuration element   
public class SimpleBehaviorExtensionElement : BehaviorExtensionElement  
    public override Type BehaviorType  
        get { return typeof(SimpleEndpointBehavior); }  

    protected override object CreateBehavior()  
         // Create the  endpoint behavior that will insert the message  
         // inspector into the client runtime  
        return new SimpleEndpointBehavior();  
<?xml version="1.0" encoding="utf-8" ?>  
            <endpoint address="http://localhost:8080/SimpleService/"   

          <behavior name="clientInspectorsAdded">  
            <simpleBehaviorExtension />  
            type="SimpleServiceLib.SimpleBehaviorExtensionElement, Host, Version=, Culture=neutral, PublicKeyToken=null"/>  

See Also

Configuring and Extending the Runtime with Behaviors