2011-05-11 86 views
0

我正在寻找一些关于WCF REST入门套件的RequestInterceptor如何工作的技术信息,但是我没有找到我想要的东西。让我们看看自定义服务主机工厂的代码片段:WCF REST StarterKit和RequestInterceptor线程安全

protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     var host = (WebServiceHost2)base.CreateServiceHost(serviceType, baseAddresses); 
     var authenticationProvider = Container.TryGetInstance<IAuthenticationProvider>(); 
     var authorizationRepository = Container.TryGetInstance<IUserFinder>(); 
     if (authenticationProvider == null) 
      authenticationProvider = new DefaultAuthenticationProvider(authorizationRepository); 
     var securityContext = new SecurityContext(); 
     host.Interceptors.Add(new AuthenticationInterceptor(authenticationProvider, securityContext)); 
     return host; 
    } 

该代码inCreateServiceHost方法只执行一次。

但是在每个HTTP请求中都会执行AuthenticationInterceptor。如您所见,AuthenticationInterceptor对SecurityContext类和IUserFinder存储库具有依赖性。

当几个HTTP请求同时发生时会发生什么?

  1. WCF是否同时执行AuthenticationInterceptor,这意味着SecurityContext和IUserFinder实例应该是线程安全的? IUserFinder依赖于数据库资源。
  2. 每个请求都被一个接一个地执行,所以AuthenticationInterceptor不能被两个不同的调用同时执行?

回答

0

我发现它是我自己的。似乎对于给定的请求,在处理下一个请求之前,所有RequestInterceptors都以线程安全的方式运行。所有请求都会排队,直到第一个请求完成所有请求拦截器。