Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WCF REST启动器工具包和RequestInterceptor线程安全_.net_Wcf_Wcf Rest_Wcf Rest Starter Kit - Fatal编程技术网

.net WCF REST启动器工具包和RequestInterceptor线程安全

.net WCF REST启动器工具包和RequestInterceptor线程安全,.net,wcf,wcf-rest,wcf-rest-starter-kit,.net,Wcf,Wcf Rest,Wcf Rest Starter Kit,我想从WCF REST初学者工具包中寻找一些关于RequestInterceptor如何工作的技术信息,但没有找到我想要的。让我们看一下从自定义服务主机工厂获取的代码片段: protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = (WebServiceHost2)base.Crea

我想从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;
    }
受保护的覆盖System.ServiceModel.ServiceHost CreateServiceHost(类型serviceType,Uri[]baseAddresses)
{
var host=(WebServiceHost2)base.CreateServiceHost(serviceType,baseAddresses);
var authenticationProvider=Container.TryGetInstance();
var authorizationRepository=Container.TryGetInstance();
if(authenticationProvider==null)
authenticationProvider=新的DefaultAuthenticationProvider(authorizationRepository);
var securityContext=new securityContext();
Add(新的AuthenticationInterceptor(authenticationProvider,securityContext));
返回主机;
}
代码IncremateServiceHost方法只执行一次

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

当多个HTTP请求同时出现时会发生什么情况

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

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