public RequestServicesContainer( HttpContext context, IServiceScopeFactory scopeFactory, IContextAccessor <HttpContext> appContextAccessor, IServiceProvider appServiceProvider) { if (scopeFactory == null) { throw new ArgumentNullException(nameof(scopeFactory)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (appContextAccessor == null) { throw new ArgumentNullException(nameof(appContextAccessor)); } AppContextAccessor = appContextAccessor; Context = context; PriorAppServices = context.ApplicationServices; PriorRequestServices = context.RequestServices; // Begin the scope Scope = scopeFactory.CreateScope(); ScopeContextAccessor = Scope.ServiceProvider.GetRequiredService <IContextAccessor <HttpContext> >(); Context.ApplicationServices = appServiceProvider; Context.RequestServices = Scope.ServiceProvider; PriorAppHttpContext = AppContextAccessor.SetValue(context); PriorScopeHttpContext = ScopeContextAccessor.SetValue(context); }