如何:检查或修改客户端上的消息

通过实现 System.ServiceModel.Dispatcher.IClientMessageInspector 并将其插入客户端运行时,可以检查或修改跨 WCF 客户端的传入或传出消息。 有关详细信息,请参阅扩展客户端。 服务上的等效功能为 System.ServiceModel.Dispatcher.IDispatchMessageInspector。 有关完整的代码示例,请参阅消息检查器示例。

检查或修改消息

  1. 实现 System.ServiceModel.Dispatcher.IClientMessageInspector 接口。

  2. 实现 System.ServiceModel.Description.IEndpointBehaviorSystem.ServiceModel.Description.IContractBehavior,具体取决于您希望在其中插入客户端消息检查器的作用域。 System.ServiceModel.Description.IEndpointBehavior 允许在终结点级别更改行为。 System.ServiceModel.Description.IContractBehavior 允许在协定级别更改行为。

  3. ClientBase<TChannel>.Open 上调用 ICommunicationObject.OpenSystem.ServiceModel.ChannelFactory<TChannel> 方法前,插入行为。 有关详细信息,请参阅使用行为配置和扩展运行时

示例

下面的代码示例按顺序演示以下各项:

  • 客户端检查器实现。

  • 插入检查器的终结点行为。

  • 一个 BehaviorExtensionElement 派生类,允许您在配置文件中添加行为。

  • 一个配置文件,它添加终结点行为,以便在客户端运行时中插入客户端消息检查器。

// Client message inspector  
public class SimpleMessageInspector : IClientMessageInspector  
{  
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)  
    {  
        // Implement this method to inspect/modify messages after a message  
        // is received but prior to passing it back to the client
        Console.WriteLine("AfterReceiveReply called");  
    }  
  
    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)  
    {  
        // Implement this method to inspect/modify messages before they
        // are sent to the service  
        Console.WriteLine("BeforeSendRequest called");  
        return null;  
    }  
}  
// Endpoint behavior  
public class SimpleEndpointBehavior : IEndpointBehavior  
{  
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)  
    {  
         // No implementation necessary  
    }  
  
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)  
    {  
        clientRuntime.MessageInspectors.Add(new SimpleMessageInspector());  
    }  
  
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)  
    {  
         // No implementation necessary  
    }  
  
    public void Validate(ServiceEndpoint endpoint)  
    {  
         // No implementation necessary  
    }  
}  
// Configuration element
public class SimpleBehaviorExtensionElement : BehaviorExtensionElement  
{  
    public override Type BehaviorType  
    {  
        get { return typeof(SimpleEndpointBehavior); }  
    }  
  
    protected override object CreateBehavior()  
    {  
         // Create the  endpoint behavior that will insert the message  
         // inspector into the client runtime  
        return new SimpleEndpointBehavior();  
    }  
}  
<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
    <system.serviceModel>  
        <client>  
            <endpoint address="http://localhost:8080/SimpleService/"
                      binding="wsHttpBinding"
                      behaviorConfiguration="clientInspectorsAdded"
                      contract="ServiceReference1.IService1"  
                      name="WSHttpBinding_IService1"/>  
        </client>  
  
      <behaviors>  
        <endpointBehaviors>  
          <behavior name="clientInspectorsAdded">  
            <simpleBehaviorExtension />  
          </behavior>  
        </endpointBehaviors>  
      </behaviors>  
      <extensions>  
        <behaviorExtensions>  
          <add  
            name="simpleBehaviorExtension"  
            type="SimpleServiceLib.SimpleBehaviorExtensionElement, Host, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"/>  
        </behaviorExtensions>  
      </extensions>  
    </system.serviceModel>  
</configuration>  

另请参阅