IClientMessageInspector 接口
定义
定义一个消息检查器对象,该对象可以添加到 MessageInspectors 集合来查看或修改消息。Defines a message inspector object that can be added to the MessageInspectors collection to view or modify messages.
public interface class IClientMessageInspector
public interface IClientMessageInspector
type IClientMessageInspector = interface
Public Interface IClientMessageInspector
示例
下面的代码示例显示一个实现,当调用该实现时将字符串写入控制台。The following code example shows an implementation that writes strings to the console when the implementation is called.
#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 在客户端终结点中插入客户端消息检查器。The following code example shows how to use an System.ServiceModel.Description.IEndpointBehavior to insert the client message inspector in the client endpoint.
#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
最后,下面的代码示例显示如何修改客户端配置文件,将终结点行为与特定的终结点一起使用。Finally, the following code example shows how to modify the client configuration file to use the endpoint behavior with a particular endpoint.
<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>
注解
实现 IClientMessageInspector 接口并将其添加到 MessageInspectors 集合,以便在消息通过 WCF 客户端对象时检查或修改消息。Implement the IClientMessageInspector interface and add it to the MessageInspectors collection to inspect or modify messages as they pass through a WCF client object. 有关详细信息,请参阅 ClientRuntime。For details, see ClientRuntime.
方法
| AfterReceiveReply(Message, Object) |
在收到回复消息之后将它传递回客户端应用程序之前,启用消息的检查或修改。Enables inspection or modification of a message after a reply message is received but prior to passing it back to the client application. |
| BeforeSendRequest(Message, IClientChannel) |
在将请求消息发送到服务之前,启用消息的检查或修改。Enables inspection or modification of a message before a request message is sent to a service. |