共用方式為


作法:檢查或修改用戶端上的訊息

您可以藉由實作 System.ServiceModel.Dispatcher.IClientMessageInspector 並將它插入用戶端執行階段,來檢查或修改流經 WCF 用戶端的傳入或傳出訊息。 如需詳細資訊,請參閱擴充用戶端。 服務上對等的功能為 System.ServiceModel.Dispatcher.IDispatchMessageInspector。 如需完整的程式碼範例,請參閱訊息偵測器範例。

檢查或修改訊息

  1. 實作 System.ServiceModel.Dispatcher.IClientMessageInspector 介面。

  2. 根據您要插入用戶端訊息偵測器的範圍,實作 System.ServiceModel.Description.IEndpointBehaviorSystem.ServiceModel.Description.IContractBehaviorSystem.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>  

另請參閱