WCF REST StarterKit 和 RequestInterceptor 线程安全

发布于 2024-11-06 09:28:18 字数 1225 浏览 4 评论 0原文

我正在寻找一些有关 WCF REST 入门套件中的 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;
    }

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

然而,在每个 HTTP 请求上都会执行 AuthenticationInterceptor。正如您所看到的,AuthenticationInterceptor 依赖于 SecurityContext 类和 IUserFinder 存储库。

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

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

I was looking for some technical information about how RequestInterceptor from the WCF REST starter kit works, but I didn't find what I wanted. Let's look at the code snippet took from the custom service host factory:

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

That code inCreateServiceHost method is only executed once.

However on every HTTP request AuthenticationInterceptor is executed. As you can see AuthenticationInterceptor has dependencies on SecurityContext class and IUserFinder repository.

What happens when several HTTP request come at the same time?

  1. Does WCF execute AuthenticationInterceptor concurrently which means that SecurityContext and IUserFinder instance should be thread safe? IUserFinder depends on data base resources.
  2. Each request is executed one after another so AuthenticationInterceptor can't be executed concurrently by two different calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2024-11-13 09:28:18

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

I found it on my own. It seems that for a given request all RequestInterceptors are run in a thread safe manner before the next request is handled. All requests are queued until the first request finish to go through all the request interceptors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文