Delen via


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 Sub AfterReceiveReply(ByRef reply As System.ServiceModel.Channels.Message, ByVal correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
  Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.")
  Console.WriteLine("Message: {0}", reply.ToString())
End Sub

Public Function BeforeSendRequest(ByRef request As System.ServiceModel.Channels.Message, ByVal channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
  Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.")
  Return Nothing
End Function
#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 Sub AddBindingParameters(ByVal endpoint As ServiceEndpoint, ByVal bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
    Return
End Sub

Public Sub ApplyClientBehavior(ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
  clientRuntime.MessageInspectors.Add(New Inspector())
End Sub

Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New Inspector())
End Sub

Public Sub Validate(ByVal endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
    Return
End Sub
#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="https://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

Reference

System.ServiceModel.Dispatcher.IClientMessageInspector
System.ServiceModel.Dispatcher.IDispatchMessageInspector

Concepts

Configuring and Extending the Runtime with Behaviors


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