方法: クライアントのメッセージを検査または変更する

System.ServiceModel.Dispatcher.IClientMessageInspector を実装し、それをクライアントのランタイムに追加することで、WCF クライアント全体での送受信メッセージの検査または変更を実行できます。 詳細については、「クライアントの拡張」を参照してください。 サービスの同等の機能は、System.ServiceModel.Dispatcher.IDispatchMessageInspector です。 コード例全体については、メッセージ インスペクターのサンプルを参照してください。

メッセージを検査または変更するには

  1. System.ServiceModel.Dispatcher.IClientMessageInspector インターフェイスを実装します。

  2. クライアント メッセージ インスペクターを挿入するスコープに応じて、System.ServiceModel.Description.IEndpointBehavior または System.ServiceModel.Description.IContractBehavior を実装します。 System.ServiceModel.Description.IEndpointBehavior を使用すると、エンドポイント レベルで動作を変更できます。 System.ServiceModel.Description.IContractBehavior を使用すると、コントラクト レベルで動作を変更できます。

  3. ClientBase<TChannel>.OpenICommunicationObject.Open メソッドまたは System.ServiceModel.ChannelFactory<TChannel> メソッドを呼び出す前に、動作を追加します。 詳細については、「動作を使用したランタイムの構成と拡張」を参照してください。

下のコード例では、次の項目を順番に示しています。

  • クライアント インスペクター実装

  • インスペクターを挿入するエンドポイント動作

  • 構成ファイルで動作を追加できるようにする BehaviorExtensionElement 派生クラス。

  • クライアント メッセージ インスペクターをクライアント ランタイムに挿入するエンドポイント動作を追加する構成ファイル。

// 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>  

関連項目