共用方式為


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>

備註

AfterReceiveReply 作 以在 WCF 用戶端物件收到回復訊息之後檢查或修改回復訊息,但在還原序列化為傳回給用戶端應用程式的物件之前。

correlationState 是使用者針對這個訊息呼叫 BeforeSendRequest 時所傳回的物件。 最好是為這個物件建立 System.Guid,以確保不會出現兩個相同的 correlationState 物件。

適用於