开发者

RequestInterceptor in WCF 4.0 REST Service Application

开发者 https://www.devze.com 2023-01-06 23:51 出处:网络
I have a WCF 4.0 REST service Application that I would like to intercept an incoming request and check a custom header.In my solution I am using the following default endpoint

I have a WCF 4.0 REST service Application that I would like to intercept an incoming request and check a custom header. In my solution I am using the following default endpoint

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

I have tried creating a IDispatchMessag开发者_C百科eInspector and corresponding BehaviorExtensionElement and adding the appropriate behaviorExtension and endPointBehavior to my web.config. What else do I have to do to make the interceptor fire?

I am assuming that my complete lack of knowledge on the real workings of WCF is killing me here. My IParameterInspector were easy to implement and works perfectly. I hoped this would be as easy to implement.


Follow up:


Since my goal of the RequestInterceptor was focused on authentication, I was able to achieve my desired result using a class derived from ServiceAuthorizationManager and added to web.config as follows.

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


To make the interceptor fire, you need to implement your custom host factory also and then add the interceptors to your service as below, after you implement your custom request interceptor using 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;
      }
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消