public async Task Invoke(HttpContext context, ITenantShellAccessor <TTenant> tenantShellAccessor, IHostingEnvironment hosting) { var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell == null) { _logger.LogDebug("Hosting Environment Middleware - Null tenant shell - No Tenant ContentRoot File Provider."); await _next(context); return; } var tenant = tenantShell?.Tenant; var tenantContentRootFileSystem = tenantShell.GetOrAddTenantContentRootFileSystem(new Lazy <ICabinet>(() => { return(_factory.GetContentRoot(tenant)); })); // Swap out IFileProvider on IHostingEnvironment var oldContentRootFilePrvovider = hosting.ContentRootFileProvider; try { _logger.LogDebug("Hosting Environment Middleware - Swapping Content Root FileProvider."); hosting.ContentRootFileProvider = tenantContentRootFileSystem.Value.FileProvider; await _next(context); } finally { _logger.LogDebug("Hosting Environment Middleware - Restoring Content Root FileProvider."); hosting.ContentRootFileProvider = tenantContentRootFileSystem.Value.FileProvider; } }
public TenantPipelineAccessor( ITenantShellAccessor <TTenant> tenantShellAccessor) { _tenantShellAccessor = tenantShellAccessor; TenantPipeline = new Func <IApplicationBuilder, IServiceProvider, RequestDelegate, ITenantMiddlewarePipelineFactory <TTenant, IApplicationBuilder, RequestDelegate>, bool, Lazy <Task <RequestDelegate> > >((appBuilder, sp, next, factory, reJoin) => { return(new Lazy <Task <RequestDelegate> >(async() => { var tenantShell = await _tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell == null) { return next; } var tenant = tenantShell?.Tenant; var tenantPipeline = tenantShell.GetOrAddMiddlewarePipeline(new Lazy <Task <RequestDelegate> >(() => { return factory.Create(appBuilder, sp, tenant, next, reJoin); })); return await tenantPipeline.Value; })); }); }
public async Task Invoke(HttpContext context, ITenantShellAccessor <TTenant> tenantShellAccessor, IHostingEnvironment hosting) { var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell != null) { var tenant = tenantShell?.Tenant; var tenantFileSystem = tenantShell.GetOrAddTenantWebRootFileSystem <TTenant>(new Lazy <ICabinet>(() => { return(_factory.GetWebRoot(tenant)); })); // swap out IFileProvider on IHostingEnvironment var oldWebRootFilePrvovider = hosting.WebRootFileProvider; try { hosting.WebRootFileProvider = tenantFileSystem.Value.FileProvider; await _next(context); } finally { hosting.ContentRootFileProvider = tenantFileSystem.Value.FileProvider; } } else { _logger.LogDebug("Null tenant shell - No Tenant Web Root File Provider."); await _next(context); } }
public async Task Invoke(HttpContext context, ITenantShellAccessor <TTenant> tenantShellAccessor) { // log.LogDebug("Using multitenancy provider {multitenancyProvidertype}.", tenantAccessor.GetType().Name); var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell == null) { await _next.Invoke(context); return; } var tenant = tenantShell?.Tenant; var lazy = tenantShell.GetOrAddContainer <TTenant>(() => _factory.Get(tenant)); var currentTenantContainer = await lazy.Value; using (var scope = currentTenantContainer.CreateNestedContainer()) { _log.LogDebug("Setting Request: {containerId} - {containerName}", scope.ContainerId, scope.ContainerName); var oldRequestServices = context.RequestServices; context.RequestServices = scope.ServiceProvider.Value; await _next.Invoke(context); _log.LogDebug("Restoring Request Container"); context.RequestServices = oldRequestServices; } }
public async Task Invoke(HttpContext context, ITenantShellAccessor <TTenant> tenantShellAccessor, IHostingEnvironment hosting) { var constructorHostingEnv = _env; var requestHostingEnv = hosting; _logger.LogDebug("Constructor env: " + _env.GetType().Name); _logger.LogDebug("Constructor env: " + hosting.GetType().Name); await _next(context); }
public TenantAccessor(ITenantShellAccessor <TTenant> tenantShellAccessor) { _tenantShellAccessor = tenantShellAccessor; CurrentTenant = new Lazy <Task <TTenant> >(async() => { var tenantShell = await _tenantShellAccessor.CurrentTenantShell?.Value; return(tenantShell?.Tenant); }); }
public async Task DisplayInfo(HttpContext context) { ILogger <Startup> logger = context.RequestServices.GetRequiredService <ILogger <Startup> >(); logger.LogDebug("App Run.."); ITenantContainerAdaptor container = context.RequestServices as ITenantContainerAdaptor; logger.LogDebug("App Run Container Is: {id}, {containerNAme}, {role}", container.ContainerId, container.ContainerName, container.Role); // Use ITenantAccessor to access the current tenant. ITenantAccessor <Tenant> tenantAccessor = container.GetRequiredService <ITenantAccessor <Tenant> >(); Tenant tenant = await tenantAccessor.CurrentTenant.Value; // This service was registered as singleton in tenant container. SomeTenantService someTenantService = container.GetService <SomeTenantService>(); // The tenant shell to access context for the tenant - even if the tenant is null ITenantShellAccessor <Tenant> tenantShellAccessor = context.RequestServices.GetRequiredService <ITenantShellAccessor <Tenant> >(); TenantShell <Tenant> tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; var myOptions = context.RequestServices.GetRequiredService <IOptions <MyOptions> >(); string tenantShellId = tenantShell == null ? "{NULL TENANT SHELL}" : tenantShell.Id.ToString(); string tenantName = tenant == null ? "{NULL TENANT}" : tenant.Name; string injectedTenantName = someTenantService?.TenantName ?? "{NULL SERVICE}"; // Accessing a content file. string fileContent = someTenantService?.GetContentFile("/Info.txt"); context.Response.ContentType = new MediaTypeHeaderValue("application/json").ToString(); var result = new { TenantShellId = tenantShellId, TenantName = tenantName, TenantScopedServiceId = someTenantService?.Id, InjectedTenantName = injectedTenantName, TenantContentFile = fileContent, OptionsFoo = myOptions.Value.Foo }; string jsonResult = JsonConvert.SerializeObject(result); await context.Response.WriteAsync(jsonResult, Encoding.UTF8); logger.LogDebug("App Run Finished.."); }
public async Task Invoke(HttpContext context, ITenantShellAccessor <TTenant> tenantShellAccessor) { var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell != null) { var tenant = tenantShell?.Tenant; var tenantPipeline = tenantShell.GetOrAddMiddlewarePipeline <TTenant>(new Lazy <RequestDelegate>(() => { return(_factory.Get(_rootApp, tenant, context.RequestServices, _next)); })); await tenantPipeline.Value(context); } else { _logger.LogDebug("Null tenant shell - No Tenant Middleware Pipeline to execute."); await _next(context); } }
public TenantContainerAccessor(ITenantShellAccessor <TTenant> tenantShellAccessor, ITenantContainerFactory <TTenant> factory) { _tenantShellAccessor = tenantShellAccessor; _containerFactory = factory; TenantContainer = new Lazy <Task <ITenantContainerAdaptor> >(async() => { var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell == null) { return(null); } var tenant = tenantShell?.Tenant; var lazy = tenantShell.GetOrAddContainer(() => { return(factory.Get(tenant)); }); return(await lazy.Value); }); }
public TenantNancyBootstrapperAccessor(ITenantShellAccessor <TTenant> tenantShellAccessor, ITenantNancyBootstrapperFactory <TTenant> factory) { _tenantShellAccessor = tenantShellAccessor; _factory = factory; Bootstrapper = new Lazy <Task <TenantContainerNancyBootstrapper <TTenant> > >(async() => { // return new Task<TTenant>(async () => //{ var tenantShell = await tenantShellAccessor.CurrentTenantShell.Value; if (tenantShell == null) { return(null); } var tenant = tenantShell?.Tenant; var lazy = tenantShell.GetOrAddNancyBootstrapper <TTenant>(() => { return(factory.Get(tenant)); }); var container = await lazy.Value; return(container); }); }