Compartir a través de


Cómo inspeccionar o modificar mensajes en el cliente

Puede inspeccionar o modificar los mensajes de entrada o de salida a través de un cliente WCF implementando System.ServiceModel.Dispatcher.IClientMessageInspector e insertándolo en el tiempo de ejecución del cliente. Para obtener más información, consulte Extensión de clientes. La característica equivalente del servicio es System.ServiceModel.Dispatcher.IDispatchMessageInspector.

Inspeccionar o modificar los mensajes

  1. Implementar la interfaz System.ServiceModel.Dispatcher.IClientMessageInspector.

  2. Implemente System.ServiceModel.Description.IEndpointBehavior o System.ServiceModel.Description.IContractBehavior según el ámbito en el que quiere insertar su inspector de mensaje de cliente con facilidad.

  3. Inserte su comportamiento antes llamandoSystem.ServiceModel.ClientBase.Open o el método System.ServiceModel.ICommunicationObject.Open en System.ServiceModel.ChannelFactory. Para obtener más información, consulte Configuración y extensión del tiempo de ejecución con comportamientos.

Ejemplo

Los siguientes ejemplos de código muestran, en orden:

  • Una implementación de inspector de cliente.

  • Un comportamiento del extremo que inserta el inspector.

  • Un archivo de configuración que carga y ejecuta el comportamiento en una aplicación cliente.

#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 "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
  <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>

Vea también

Referencia

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

Conceptos

Configuración y extensión del tiempo de ejecución con comportamientos