WCF REST StarterKit 和 RequestInterceptor 线程安全
我正在寻找一些有关 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请求同时到来时会发生什么?
- WCF 是否同时执行 AuthenticationInterceptor 这意味着 SecurityContext 和 IUserFinder 实例应该是线程安全的? IUserFinder 依赖于数据库资源。
- 每个请求都依次执行,因此 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?
- Does WCF execute AuthenticationInterceptor concurrently which means that SecurityContext and IUserFinder instance should be thread safe? IUserFinder depends on data base resources.
- Each request is executed one after another so AuthenticationInterceptor can't be executed concurrently by two different calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到的。似乎对于给定的请求,在处理下一个请求之前,所有 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.