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.

Example

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; }

  <client>
      <endpoint 
        address="http://localhost:8080/SampleService" 
        behaviorConfiguration="clientInspectorsAdded" 
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_ISampleService" 
        contract="ISampleService"
        name="WSHttpBinding_ISampleService"
      >
      </endpoint>
  </client>
<behaviors>
  <endpointBehaviors>
    <behavior name="clientInspectorsAdded">
      <clientInterceptors />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add 
      name="clientInterceptors" 
      type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
  />
  </behaviorExtensions>
</extensions>

See Also


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

Community Additions

ADD
Show:
© 2015 Microsoft