如何:检查或修改客户端的消息
通过实现 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