【问题标题】:WCF REST StarterKit and RequestInterceptor thread safetyWCF REST StarterKit 和 RequestInterceptor 线程安全
【发布时间】:2011-08-23 04:03:50
【问题描述】:

我一直在寻找有关 WCF REST 入门工具包中的 RequestInterceptor 如何工作的技术信息,但没有找到我想要的。让我们看看sn-p从自定义服务宿主工厂获取的代码:

    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;
    }

CreateServiceHost 方法中的代码只执行一次。

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

当多个 HTTP 请求同时到来时会发生什么?

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

【问题讨论】:

    标签: .net wcf wcf-rest wcf-rest-starter-kit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-03-27
      • 2011-08-26
      • 2011-03-16
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多