Getting Started
MultiTenant is designed to be easy to use and follows standard .NET conventions as much as possible. This introduction assumes a typical ASP.NET Core use case, but any application using .NET dependency injection can work with the library.
Installation
MultiTenant is split into focused NuGet packages so you only reference what you need. For a typical ASP.NET
Core app install Finbuckle.MultiTenant.AspNetCore:
dotnet add package Finbuckle.MultiTenant.AspNetCore
Release builds show warning FBMT0001 until you accept the Open Source Maintenance Fee EULA by setting
<AcceptFinbuckleEULA>true</AcceptFinbuckleEULA> in your project. See
Open Source Maintenance Fee for details.
Basic Configuration
MultiTenant is simple to get started with. Below is a sample app configured to use the subdomain as the tenant
identifier and the app's configuration (
most likely from an appsettings.json file) as the source of tenant details.
using Finbuckle.MultiTenant;
var builder = WebApplication.CreateBuilder(args);
// add app services...
// add MultiTenant services
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore();
var app = builder.Build();
// add the MultiTenant middleware
app.UseMultiTenant<string>();
// add other middleware...
app.Run();
That's all that is needed to get going. Let's break down each line:
builder.Services.AddMultiTenant<TenantInfo, string>()
This line registers the base services and designates TenantInfo as the class that will hold tenant information at
runtime and string as the tenant id type.
AddMultiTenant<TTenantInfo, TId> has two type parameters: TTenantInfo must implement ITenantInfo<TId> and holds
basic information about the tenant such as its id and an identifier, while TId is the type of the tenant id. TId
must be IEquatable<TId> — string, int, and Guid are common choices. TenantInfo<TId> is provided as a basic
implementation class, but any implementation of ITenantInfo<TId> can be used if more properties are needed. For
example, AddMultiTenant<AppTenantInfo, Guid>() uses Guid tenant ids.
See Core Concepts for more information on TenantInfo and tenant id types.
.WithHostStrategy()
The line tells the app that our "strategy" to determine the request tenant will be to look at the request host, which defaults to extracting the subdomain as a tenant identifier.
See Strategies for more information.
.WithConfigurationStore()
This line tells the app that information for all tenants are in the appsettings.json file used for app configuration.
If a tenant in the store has the identifier found by the strategy, the tenant will be successfully resolved for the
current request.
A minimal configuration entry looks like this:
{
"Finbuckle:MultiTenant:Stores:ConfigurationStore": {
"Tenants": [
{
"Id": "93f330717e5d4f039cd05da312d559cc",
"Identifier": "initech",
"Name": "Initech"
}
]
}
}
See Stores for more information.
MultiTenant comes with a collection of strategies and store types that can be mixed and matched in various ways.
app.UseMultiTenant<TId>()
This line configures the middleware which resolves the tenant using the registered strategies, stores, and other
settings. Be sure to call it before other middleware which will use per-tenant functionality, such as
UseAuthentication() or UseAuthorization() so those components see the resolved tenant.
Basic Usage
With the services and middleware configured, access information for the current tenant from the TenantInfo property on
the ITenantContext<TTenantInfo, TId> object accessed from the GetTenantContext<TTenantInfo, TId> extension method:
var tenantInfo = HttpContext.GetTenantContext<TenantInfo, string>().TenantInfo;
if(tenantInfo != null)
{
var tenantId = tenantInfo.Id;
var identifier = tenantInfo.Identifier;
}
The type of the TenantInfo property depends on the type passed when calling AddMultiTenant<TTenantInfo, TId> during
configuration. If the current tenant could not be determined then TenantInfo will be null.
For access without the concrete tenant info type in ASP.NET Core, use HttpContext.TenantContext<TId>(). To read only
the current tenant as ITenantInfo<TId>, use HttpContext.TenantInfo<TId>().
The TenantInfo instance and the typed instance are also available using the ITenantContext<TTenantInfo, TId>
interface which is available via dependency injection.
See Configuration and Usage for more information.
Advanced Usage
The library builds on this basic functionality to provide a variety of higher level features. See the documentation for more details:
- ASP.NET Core Integration — middleware options, bypassing, short-circuiting,
HttpContexthelpers - Per-tenant Options
- Per-tenant Authentication
- Entity Framework Core Data Isolation
- ASP.NET Core Identity Data Isolation
Samples
A variety of sample projects are available in the samples directory.
Important Considerations
- The type parameter passed to
AddMultiTenant<TTenantInfo, TId>()determines theITenantInfo<Tid>implementation used throughout the app. Choose or define a class that fits your tenant data model. ITenantContext<TTenantInfo, TId>is an ambient service — the current tenant is held in anAsyncLocaland flows with the asynchronous execution context. In ASP.NET Core the scope begins automatically at the start of each request's pipeline, and the middleware populates it. In other app models you begin the scope yourself withBeginTenantScopeafter resolving the tenant (see .NET Generic Host Integration).TenantInfocan only be set once per scope. The middleware handles this in web apps, but be aware of the constraint if you callSetTenantInfoorBeginTenantScopemanually.- Middleware ordering is critical:
UseMultiTenant<TId>()must come beforeUseAuthentication(),UseAuthorization(), and any middleware that reads per-tenant options or services. - For web apps, prefer the
HttpContextextension members (GetTenantContext<T, TId>(),GetTenantInfo<T, TId>(),TenantContext<TId>(),TenantInfo<TId>()) over injectingITenantContextdirectly, as they always reflect the middleware's state.
See Also
- Core Concepts —
ITenantInfo<TId>, strategies, stores - Configuration and Usage — all registration options
- ASP.NET Core Integration — middleware and
HttpContexthelpers - .NET Generic Host Integration — non-web scenarios