![]() |
VOOZH | about |
dotnet add package Autofac.AspNetCore.Multitenant --version 8.0.0
NuGet\Install-Package Autofac.AspNetCore.Multitenant -Version 8.0.0
<PackageReference Include="Autofac.AspNetCore.Multitenant" Version="8.0.0" />
<PackageVersion Include="Autofac.AspNetCore.Multitenant" Version="8.0.0" />Directory.Packages.props
<PackageReference Include="Autofac.AspNetCore.Multitenant" />Project file
paket add Autofac.AspNetCore.Multitenant --version 8.0.0
#r "nuget: Autofac.AspNetCore.Multitenant, 8.0.0"
#:package Autofac.AspNetCore.Multitenant@8.0.0
#addin nuget:?package=Autofac.AspNetCore.Multitenant&version=8.0.0Install as a Cake Addin
#tool nuget:?package=Autofac.AspNetCore.Multitenant&version=8.0.0Install as a Cake Tool
ASP.NET Core support for multitenant DI via Autofac.Multitenant.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
ASP.NET Core default RequestServicesContainerMiddleware is where the per-request lifetime scope usually gets generated. However, its constructor is where it wants the IServiceScopeFactory that will be used later during the request to create the request lifetime scope.
Unfortunately, that means the IServiceScopeFactory is created/resolved at the point when the request comes in, long before an HttpContext is set in any IHttpContextAccessor. The result is the scope factory ends up coming from the default tenant scope, before a tenant can be identified, and per-request services will later all come from the default tenant. Multitenancy fails.
This package provides a different request services middleware that ensures the IHttpContextAccessor.HttpContext is set and defers creation of the request lifetime scope until as late as possible so anything needed for tenant identification can be established.
When creating your application host, use the multitenant service provider factory. Your Startup will register common dependencies, but you'll need to provide a static method to initialize the tenant-specific overrides.
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(MultitenantContainerSetup.ConfigureMultitenantContainer))
.ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.UseStartup<Startup>())
.Build();
In your Startup class, make sure to use the multitenant request services middleware and register your common dependencies.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add the multitenant request services handler.
services
.AddAutofacMultitenantRequestServices()
.AddControllers();
}
public void ConfigureContainer(ContainerBuilder builder)
{
// Register tenant-shared dependencies and defaults.
builder.RegisterType<CommonDependency>()
.As<IDependency>()
.InstancePerLifetimeScope();
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(builder => builder.MapControllers());
}
}
Provide a method to override things for tenant-specific dependencies. This is what gets passed to the multitenant service provider factory.
public static class MultitenantContainerSetup
{
public static MultitenantContainer ConfigureMultitenantContainer(IContainer container)
{
// Define how you're going to identify tenants.
var strategy = new QueryStringTenantIdentificationStrategy(
container.Resolve<IHttpContextAccessor>(),
container.Resolve<ILogger<QueryStringTenantIdentificationStrategy>>());
// Create the multitenant container.
var multitenantContainer = new MultitenantContainer(strategy, container);
// Register tenant overrides.
multitenantContainer.ConfigureTenant(
"some-tenant",
cb => cb
.RegisterType<OverrideDependency>()
.As<IDependency>()
.WithProperty("Id", "some-tenant")
.InstancePerLifetimeScope());
// Return the built container for use in the app.
return multitenantContainer;
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on Autofac.AspNetCore.Multitenant:
| Package | Downloads |
|---|---|
|
Autofac.AspNetCore.Extensions
ASP.NET Core library with IWebHostBuilder Single Tenant and Multitenant Autofac Extensions |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.0 | 1,175,419 | 3/12/2024 |
| 7.0.0 | 288,242 | 5/5/2023 |
| 6.0.0 | 475,625 | 7/18/2022 |
| 5.1.0 | 318,205 | 11/15/2021 |
| 5.0.0 | 2,697,767 | 12/20/2020 |
| 4.0.1 | 27,830 | 10/6/2020 |
| 4.0.0 | 1,960 | 9/30/2020 |
| 3.0.2 | 148,606 | 8/5/2020 |
| 3.0.1 | 563,355 | 4/7/2020 |
| 3.0.0 | 65,781 | 1/28/2020 |
| 2.0.1 | 170,908 | 11/12/2019 |
| 2.0.0 | 21,790 | 9/24/2019 |
| 2.0.0-rc1 | 747 | 9/5/2019 |
| 1.1.0 | 68,022 | 4/16/2019 |
| 1.0.2 | 82,554 | 7/12/2018 |
| 1.0.1 | 5,269 | 6/1/2018 |
| 1.0.1-rc1 | 1,701 | 5/7/2018 |
| 1.0.0 | 13,016 | 8/18/2017 |
Release notes are at https://github.com/autofac/Autofac.AspNetCore.Multitenant/releases