Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How to: Inspect or Modify Messages on the Client
Collapse the table of content
Expand the table of content

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.

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 easily insert your client message inspector.

  3. Insert your behavior prior by calling the System.ServiceModel.ClientBase.Open or the System.ServiceModel.ICommunicationObject.Open method on the System.ServiceModel.ChannelFactory. 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 configuration file that loads and runs the behavior in a client application.

#region IClientMessageInspector Members
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.");
  Console.WriteLine("Message: {0}", reply.ToString());

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
  Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.");
  return null;

#region IEndpointBehavior Members
public void AddBindingParameters(
  ServiceEndpoint endpoint, BindingParameterCollection bindingParameters
) { return; }

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

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new Inspector());

public void Validate(ServiceEndpoint endpoint){ return; }

    <behavior name="clientInspectorsAdded">
      <clientInterceptors />
      type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=, Culture=neutral, PublicKeyToken=null"

See Also

© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21

Community Additions

© 2015 Microsoft