/// <summary> /// Invokes the middleware using the specified context. /// </summary> /// <param name="context"> /// The request context to process through the middleware. /// </param> /// <returns> /// A <see cref="Task"/> to await for completion of the operation. /// </returns> public async Task Invoke(HttpContext context) { // If there isn't already an HttpContext set on the context // accessor for this async/thread operation, set it. This allows // tenant identification to use it. if (this._contextAccessor.HttpContext == null) { this._contextAccessor.HttpContext = context; } var container = this._multitenantContainerAccessor(); if (container == null) { throw new InvalidOperationException(Properties.Resources.NoMultitenantContainerAvailable); } using (var feature = new RequestServicesFeature(container.Resolve <IServiceScopeFactory>())) { var existingFeature = context.Features.Get <IServiceProvidersFeature>(); try { context.Features.Set <IServiceProvidersFeature>(feature); await this._next.Invoke(context); } finally { context.Features.Set(existingFeature); } } }
public async Task Invoke(HttpContext httpContext) { var existingFeature = httpContext.Features.Get <IServiceProvidersFeature>(); // All done if request services is set if (existingFeature?.RequestServices != null) { await _next.Invoke(httpContext); return; } using (var feature = new RequestServicesFeature(httpContext, _scopeFactory)) { try { httpContext.Features.Set <IServiceProvidersFeature>(feature); await _next.Invoke(httpContext); } finally { httpContext.Features.Set(existingFeature); } } }
public async Task Invoke(HttpContext context) { var existingFeature = context.Features.Get <IServiceProvidersFeature>(); using (var feature = new RequestServicesFeature(new ServiceProviderScopeFactory(_app.ApplicationServices))) { try { context.Features.Set <IServiceProvidersFeature>(feature); await _next.Invoke(context); } finally { context.Features.Set(existingFeature); } } }
/// <summary> /// Invokes the middleware using the specified context. /// </summary> /// <param name="httpContext"> /// The request context to process through the middleware. /// </param> /// <returns> /// A <see cref="Task"/> to await for completion of the operation. /// </returns> public async Task Invoke(HttpContext httpContext) { Debug.Assert(httpContext != null, nameof(httpContext)); TenantContext <TTenant> tenantContext = httpContext.GetTenantContext <TTenant>(); if (tenantContext != null) { IServiceProvidersFeature existingRequestServices = httpContext.Features.Get <IServiceProvidersFeature>(); using (RequestServicesFeature feature = new RequestServicesFeature(httpContext, serviceFactoryForMultitenancy.Build(tenantContext).GetRequiredService <IServiceScopeFactory>())) { // Replace the request IServiceProvider created by IServiceScopeFactory httpContext.RequestServices = feature.RequestServices; ILog <MultitenancyRequestServicesContainerMiddleware <TTenant> > log = httpContext.RequestServices.GetRequiredService <ILog <MultitenancyRequestServicesContainerMiddleware <TTenant> > >(); log.Log(Logging.LibLog.LogLevel.Info, () => $"IServiceProvider is successfully set for tenant {tenantContext.Id}"); await next.Invoke(httpContext).ConfigureAwait(false); } } }