Instrukcje: Inspekcja lub modyfikowanie komunikatów na kliencie

Możesz sprawdzić lub zmodyfikować przychodzące lub wychodzące komunikaty w kliencie programu WCF, implementując System.ServiceModel.Dispatcher.IClientMessageInspector element i wstawiając go do środowiska uruchomieniowego klienta. Aby uzyskać więcej informacji, zobacz Rozszerzanie klientów. Równoważną funkcją usługi jest System.ServiceModel.Dispatcher.IDispatchMessageInspector. Pełny przykład kodu można znaleźć w przykładzie Inspektorzy komunikatów .

Aby sprawdzić lub zmodyfikować komunikaty

  1. Zaimplementuj interfejs System.ServiceModel.Dispatcher.IClientMessageInspector.

  2. Zaimplementuj System.ServiceModel.Description.IEndpointBehavior element lub System.ServiceModel.Description.IContractBehavior w zależności od zakresu, w którym chcesz wstawić inspektora komunikatów klienta. System.ServiceModel.Description.IEndpointBehavior umożliwia zmianę zachowania na poziomie punktu końcowego. System.ServiceModel.Description.IContractBehavior umożliwia zmianę zachowania na poziomie kontraktu.

  3. Wstaw zachowanie przed wywołaniem ClientBase<TChannel>.Open metody lub ICommunicationObject.Open w metodzie System.ServiceModel.ChannelFactory<TChannel>. Aby uzyskać szczegółowe informacje, zobacz Konfigurowanie i rozszerzanie środowiska uruchomieniowego przy użyciu zachowań.

Przykład

W poniższych przykładach kodu pokazano w następującej kolejności:

  • Implementacja inspektora klienta.

  • Zachowanie punktu końcowego, które wstawia inspektora.

  • A BehaviorExtensionElement— klasa pochodna, która umożliwia dodanie zachowania w pliku konfiguracji.

  • Plik konfiguracji, który dodaje zachowanie punktu końcowego, który wstawia inspektora komunikatów klienta do środowiska uruchomieniowego klienta.

// 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" ?>  
<configuration>  
    <system.serviceModel>  
        <client>  
            <endpoint address="http://localhost:8080/SimpleService/"
                      binding="wsHttpBinding"
                      behaviorConfiguration="clientInspectorsAdded"
                      contract="ServiceReference1.IService1"  
                      name="WSHttpBinding_IService1"/>  
        </client>  
  
      <behaviors>  
        <endpointBehaviors>  
          <behavior name="clientInspectorsAdded">  
            <simpleBehaviorExtension />  
          </behavior>  
        </endpointBehaviors>  
      </behaviors>  
      <extensions>  
        <behaviorExtensions>  
          <add  
            name="simpleBehaviorExtension"  
            type="SimpleServiceLib.SimpleBehaviorExtensionElement, Host, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"/>  
        </behaviorExtensions>  
      </extensions>  
    </system.serviceModel>  
</configuration>  

Zobacz też