HOW TO:檢查或修改用戶端上的訊息
您可以實作 System.ServiceModel.Dispatcher.IClientMessageInspector 並將它插入用戶端執行階段,以檢查或修改 WCF 用戶端內傳入或傳出的訊息。如需詳細資訊,請參閱擴充用戶端。服務上對等的功能為 System.ServiceModel.Dispatcher.IDispatchMessageInspector。
檢查或修改訊息
請實作 System.ServiceModel.Dispatcher.IClientMessageInspector 介面。
根據您要輕鬆插入用戶端訊息偵測器的範圍,實作 System.ServiceModel.Description.IEndpointBehavior 或 System.ServiceModel.Description.IContractBehavior。
在 System.ServiceModel.ChannelFactory 上呼叫 System.ServiceModel.ClientBase.Open 或 System.ServiceModel.ICommunicationObject.Open 方法之前,請先插入您的行為。如需詳細資訊,請參閱使用行為來設定與擴充執行階段。
範例
下列程式碼範例會依序顯示:
用戶端偵測器實作。
插入偵測器的端點行為。
在用戶端應用程式中載入及執行此行為的組態檔。
#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>
另請參閱
參考
System.ServiceModel.Dispatcher.IClientMessageInspector
System.ServiceModel.Dispatcher.IDispatchMessageInspector