2011-06-10 58 views
9

我正在嘗試創建和配置Message Inspector以執行WCF Rest HTTP請求的某些身份驗證。我正在使用4.0,所以試圖避開WCF入門套件,儘管我已經設法按照我想要的方式獲得舊的RequestInterceptor。使用RequestInterceptor的問題是我失去了我真正想保留的WebHttpBehavior提供的automaticFormatSelectionEnabled特性。如何在WCF REST 4.0中使用StandardEndpoints時配置MessageInspector

所以我的問題是如何配置Message Inspector的方式,我仍然使用WebHttpBehavior並保留它的功能。

我的web.config看起來是這樣,你可以處理這是創建3個對象

<standardEndpoints> 
    <webHttpEndpoint> 
    <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. --> 
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" /> 
    <!-- Disable the help page for the directory end point--> 
    <standardEndpoint name="DirectoryEndpoint"/> 
    </webHttpEndpoint> 
</standardEndpoints> 

回答

19

的一種方式。

  1. 的消息檢查,負責分析 請求/響應
  2. 的服務行爲,自動噴射檢查員到 管道
  3. 配置部分,允許在 捲筒紙將要使用的行爲.config

首先通過實現IDispatchMessageInspector 並將您的驗證代碼放在After中創建消息檢查器ReceiveRequest方法:

public class HmacVerificationInspector : IDispatchMessageInspector 
{ 

    #region IDispatchMessageInspector Members 

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, 
     System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 
    { 
      MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue); 
      request = buffer.CreateMessage(); 
      Message dupeRequest = buffer.CreateMessage(); 

      ValidateHmac(dupeRequest); 

      buffer.Close(); 

     return null; 
    } 

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, 
     object correlationState) 
    { 


    } 

    #endregion 
} 

讀取消息時創建消息的緩衝副本很重要。消息只能打開一次,而不會創建副本會導致管道問題。如果失敗,我的ValidateHmac實現會引發異常。這可以防止實際的服務被調用。

其次,爲您的檢查員創建一個行爲。我們將使用該行爲將檢查器注入WCF運行時。要創建的行爲,得出從IEndpointBehavior一類,所以它看起來像這樣

public class HmacVerificationBehavior : IEndpointBehavior 
    { 
     #region IEndpointBehavior Members 

     public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
     { 

     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
     { 

     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) 
     { 
      HmacVerificationInspector inspector = new HmacVerificationInspector(); 

      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector); 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 

     } 

     #endregion 
    } 

通知我創造我檢查(HmacVerificationInspector)的新實例和編程其注入運行。

最後,最後一步是創建一個配置部分。我們可以使用它來應用web配置中的行爲(因此可以通過配置打開和關閉它)。創建一個新的類,然後從BehaviorExtensionElement和IServiceBehavior接口繼承:

public class HmacVerificationConfigurationSection : BehaviorExtensionElement, IServiceBehavior 
{ 
    #region IServiceBehavior Members 

    public void AddBindingParameters(ServiceDescription serviceDescription, 
     System.ServiceModel.ServiceHostBase serviceHostBase, 
     System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, 
     System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
    { 

    } 

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase) 
    { 

    } 

    public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase) 
    { 

    } 

    #endregion 

    public override Type BehaviorType 
    { 
     get { return typeof(HmacVerificationBehavior); } 
    } 

    protected override object CreateBehavior() 
    { 
     return new HmacVerificationBehavior(); 
    } 
} 

現在,使用檢查,以下內容添加到你的web.config

(你可以爲你的擴展到任何你想要設置的名稱)
<system.serviceModel> 
     <extensions> 
      <behaviorExtensions> 
       <add name="hmacVerification" type="NamespaceHere.HmacVerificationConfigurationSection, AssembleyHere, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
      </behaviorExtensions> 
     </extensions> 
     <services> 
      <service name="MySecureService"> 
       <endpoint address="" binding="webHttpBinding" contract="IMySecureService" behaviorConfiguration="web" /> 
      </service> 
     </services> 
     <behaviors> 
      <endpointBehaviors> 
       <behavior name="web"> 
        <webHttp automaticFormatSelectionEnabled="true" />   
        <hmacVerification /> 
       </behavior> 
      </endpointBehaviors> 
      <serviceBehaviors> 
       <behavior name=""> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 
    </system.serviceModel> 

幾件事,首先在行爲擴展中註冊配置部分。接下來,您將該配置用作端點行爲,然後該端點行爲將自動注入檢查器,並且該端點的所有請求都將通過檢查器運行。如果您想關閉檢查器,請移除標籤或選擇不同的端點行爲。請注意使用webHttp行爲(這將允許您保留automaticFormatSelectionEnabled。

希望這會有幫助

+0

你知道我認爲這可能已經過時。 – control 2013-03-27 19:12:09

+0

相反,我認爲它非常有幫助。謝謝。 – Barun 2014-04-11 11:04:27

+0

更好的帖子關於wcf消息我讀過很多時間。非常感謝你。 – 2016-04-18 07:20:21