private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { ScopeContextAccessor.SetValue(PriorScopeHttpContext); AppContextAccessor.SetValue(PriorAppHttpContext); Context.RequestServices = PriorRequestServices; Context.ApplicationServices = PriorAppServices; } if (Scope != null) { Scope.Dispose(); Scope = null; } Context = null; PriorAppServices = null; PriorRequestServices = null; ScopeContextAccessor = null; AppContextAccessor = null; PriorAppHttpContext = null; PriorScopeHttpContext = null; disposedValue = true; } }
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); }