IClientMessageInspector.AfterReceiveReply(Message, Object) メソッド

定義

応答メッセージを受信した後、それをクライアント アプリケーションに返信する前に、メッセージの検査と変更を実行できるようにします。

public:
 void AfterReceiveReply(System::ServiceModel::Channels::Message ^ % reply, System::Object ^ correlationState);
public void AfterReceiveReply (ref System.ServiceModel.Channels.Message reply, object correlationState);
abstract member AfterReceiveReply : Message * obj -> unit
Public Sub AfterReceiveReply (ByRef reply As Message, correlationState As Object)

パラメーター

reply
Message

各種型に変換され、クライアント アプリケーションに戻されるメッセージ。

correlationState
Object

相関状態データ。

呼び出されたときにコンソールに文字列を出力する実装を、次のコード例に示します。

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

System.ServiceModel.Description.IEndpointBehavior を使用してクライアント メッセージ インスペクターをクライアントのエンドポイントに挿入する方法を、次のコード例に示します。

#region IEndpointBehavior Members
public void AddBindingParameters(
  ServiceEndpoint endpoint, BindingParameterCollection bindingParameters
) { return; }

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
  clientRuntime.MessageInspectors.Add(new Inspector());
  foreach (ClientOperation op in clientRuntime.Operations)
    op.ParameterInspectors.Add(new Inspector());
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new Inspector());
  foreach (DispatchOperation op in endpointDispatcher.DispatchRuntime.Operations)
    op.ParameterInspectors.Add(new Inspector());
}

public void Validate(ServiceEndpoint endpoint){ return; }
#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())
        For Each op As ClientOperation In clientRuntime.Operations
            op.ParameterInspectors.Add(New Inspector())
        Next op
    End Sub

    Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As  _
                                     EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New Inspector())
        For Each op As DispatchOperation In endpointDispatcher.DispatchRuntime.Operations
            op.ParameterInspectors.Add(New Inspector())
        Next op
    End Sub

Public Sub Validate(ByVal endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
    Return
End Sub

最後に、特定のエンドポイントでエンドポイント動作を使用するようにクライアント構成ファイルを変更する方法を、次のコード例に示します。

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

注釈

WCF クライアント オブジェクトによって受信された後、クライアント アプリケーションに返されるオブジェクトに逆シリアル化される前に、応答メッセージを検査または変更するために実装 AfterReceiveReply します。

correlationState は、このメッセージに対して BeforeSendRequest が呼び出されたときに、ユーザーによって返されるオブジェクトです。 2 つの System.Guid オブジェクトが決して同じにならないように、これを correlationState にすることをお勧めします。

適用対象