Я искал техническую информацию о том, как работает RequestInterceptor из стартового набора WCF REST, но не нашел того, что хотел. Давайте посмотрим на фрагмент кода, взятый из фабрики узлов пользовательского сервиса:
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-запросов?
- Выполняет ли WCF AuthenticationInterceptor одновременно, что означает, что экземпляры SecurityContext и IUserFinder должны быть потокобезопасными? IUserFinder зависит от ресурсов базы данных.
- Каждый запрос выполняется один за другим, поэтому AuthenticationInterceptor не может выполняться одновременно двумя разными вызовами?