【问题标题】:RequestInterceptor in WCF 4.0 REST Service ApplicationWCF 4.0 REST 服务应用程序中的 RequestInterceptor
【发布时间】:2011-03-16 00:46:18
【问题描述】:

我有一个 WCF 4.0 REST 服务应用程序,我想拦截传入请求并检查自定义标头。在我的解决方案中,我使用以下默认端点

<standardEndpoints>
 <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
 </webHttpEndpoint>
</standardEndpoints>

我已尝试创建 IDispatchMessageInspector 和相应的 BehaviorExtensionElement,并将适当的 behaviorExtension 和 endPointBehavior 添加到我的 web.config。我还需要做什么才能让拦截器着火?

我假设我完全缺乏对 WCF 实际运作的了解,这让我很生气。我的 IParameterInspector 很容易实现并且运行良好。我希望这会很容易实现。

【问题讨论】:

    标签: wcf c#-4.0


    【解决方案1】:

    跟进:


    由于我的 RequestInterceptor 目标专注于身份验证,因此我能够使用派生自 ServiceAuthorizationManager 并添加到 web.config 的类来实现我想要的结果,如下所示。

     <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- This behavior enables Auth Token Verification -->
          <serviceAuthorization serviceAuthorizationManagerType="Something.Service.Authorization, Something.Service" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    【讨论】:

    • 您有用于身份验证的示例拦截器吗?
    【解决方案2】:

    要使拦截器触发,您还需要实现自定义主机工厂,然后在使用 Microsoft.ServiceModel.Web.RequestInterceptor 实现自定义请求拦截器后,将拦截器添加到您的服务中,如下所示

     public class MyCustomHostFactory : ServiceHostFactory
       {
          protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
          {
             var serviceHost = new WebServiceHost2(serviceType, true, baseAddresses);
             RequestInterceptor interceptor = MySolution.RequestInterceptorFactory.Create();
             serviceHost.Interceptors.Add(interceptor);
             return serviceHost;
          }
       }
    

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 2011-08-23
      • 2011-07-21
      • 2014-11-11
      • 1970-01-01
      • 2011-03-27
      • 2014-01-21
      • 2013-02-14
      相关资源
      最近更新 更多