Core Concepts
The library uses standard .NET Core conventions and most of the internal details are abstracted away from app code. However, there are a few important specifics to be aware of. The items below make up the foundation of the library.
ITenantInfo and TenantInfo
A TenantInfo instance contains information about a tenant. Often this will be the "current" tenant in the context of an
app. The type of these instances must implement ITenantInfo<TId> which defines properties for Id (of type TId) and
Identifier (a string). TenantInfo<TId> is a basic implementation provided by the library with those two properties.
AddMultiTenant<TTenantInfo, TId> takes two type parameters: TTenantInfo (the ITenantInfo<TId> implementation used
throughout the library and app) and TId (the type of the tenant Id). TId must be IEquatable<TId>, so common
choices are string, int, and Guid. For example, AddMultiTenant<TenantInfo, string>() uses string ids while
AddMultiTenant<AppTenantInfo, Guid>() uses Guid ids.
Idis a unique id for a tenant in your app and should never change. It must be non-default:default(TId)—0,Guid.Empty, or a null/whitespacestring— is treated as no tenant and is rejected (aMultiTenantExceptionis thrown when adding, updating, resolving, or assigning such a tenant, and anArgumentExceptionwhen looking one up by a default id).Identifieris the value used to actually resolve a tenant and should have a syntax compatible for your app (i.e. no crazy symbols in a web app where the identifier will be part of the URL). It must be non-empty. UnlikeId,Identifiercan be changed if necessary by updating the tenant with a new instance (see the tenant info contract below).
The library provides TenantInfo<TId> as a base implementation. Your app can define a custom class implementing
ITenantInfo<TId> (or inheriting from TenantInfo<TId>) and add custom properties as needed. It is recommended to
keep these classes lightweight since they are often queried. Keep heavier associated data in an external area that can
be pulled in when needed via the tenant Id.
Previous versions of
TenantInfoincluded a connection string property. If needed simply add it to your customTenantInfoderived class.
Tenant info contract
ITenantInfo<TId> is the only contract the library depends on. It is read-only from the library's point of view:
the library never writes to a tenant info instance after it has been constructed and never forces a particular shape
onto your type. All of the following are supported and covered by the test suite:
// plain mutable properties
public class AppTenantInfo : ITenantInfo<string>
{
public string Id { get; set; } = string.Empty;
public string Identifier { get; set; } = string.Empty;
public string? Name { get; set; }
}
// init-only properties (the shape of the built-in TenantInfo<TId>)
public class AppTenantInfo : TenantInfo<string>
{
public string? Name { get; init; }
}
// constructor-only, fully immutable, not derived from TenantInfo and not a record
public sealed class AppTenantInfo(string id, string identifier, string? name = null) : ITenantInfo<string>
{
public string Id { get; } = id;
public string Identifier { get; } = identifier;
public string? Name { get; } = name;
}
In return the library asks for two things:
Idnever changes. It is the tenant's identity; tenant equality isIdequality.Identifiermay change, but only through the store. Create a new instance carrying the newIdentifierand pass it toTenantManager<TTenantInfo, TId>.UpdateAsync; the store re-keys its lookups and hands out the new instance from then on. Never changeIdentifierin place on an instance the library already holds (a store's map, a cache entry, or the current tenant context), because those are keyed by the value captured when the tenant was added or updated and cannot notice the change. WhetherIdentifiermatching is case-sensitive depends on the store:InMemoryStoreandConfigurationStoreare case-insensitive, while the EF Core and HTTP remote stores and the store caches follow their database, remote server, or cache. See MultiTenant Stores for each store.
Mutating other properties of your own type is your business, but keep in mind the same instance may be shared by caches and by the current request.
The library enforces its side of the contract:
- The active tenant of an ambient tenant scope can only be set once (see below); there is no API to replace it.
- Built-in stores key their lookups by the
Id/Identifiervalues captured when a tenant is added or updated, never by re-reading the stored instance, so the store cannot be corrupted by mutation of an instance it handed out. - Stores that must construct tenant instances themselves (
ConfigurationStoreandEchoStore) support constructor-only types out of the box or through a factory overload. See MultiTenant Stores for details.
ITenantContext and the ambient tenant scope
ITenantContext<TTenantInfo, TId> (and its base ITenantContext<TId>) holds the current tenant. The
default implementation is AmbientTenantContext<TTenantInfo, TId>, obtained from dependency injection.
- The context is registered as a singleton but is ambient — the current tenant is stored in an
AsyncLocal, so it flows with the current asynchronous execution context rather than with the DI scope. Each logical unit of work (an HTTP request, a dequeued message, a console run) gets its own ambient tenant by first callingBeginScope()/BeginTenantScope(). - You establish a scope with
ITenantScopeProvider.BeginScope()or, more commonly, theIServiceProvider.BeginTenantScope(tenantInfo)extension, which begins a scope and sets the tenant in one call. In ASP.NET Core a scope begins automatically at the start of each request's pipeline (see ASP.NET Core Integration); in other hosts you call it yourself (see .NET Generic Host Integration). ITenantScopeProvider.EndScope()ends the current scope, after which reading or setting the tenant throws until a new scope begins. Work that captured the scope before it ended, such as a background task started during a request, keeps the scope it captured.ITenantScopeProvider.HasScopereports whether a scope is active. ASP.NET Core ends the scope automatically when the request'sHttpContextis disposed.- If you replace the default registration, register
ITenantScopeProvideras a singleton that shares its ambient state withITenantContext, as the defaultAmbientTenantContextregistration does. In ASP.NET Core,UseMultiTenant<TId>()throws aMultiTenantExceptionat startup if it resolves to different instances. - Accessing
TenantInfobefore a scope has been established throws aMultiTenantException("No ambient tenant scope has been established."). - The
TenantInfoproperty holds the current tenant info. Within a scope it can only be set once — attempting to set it a second time throws aMultiTenantException. A tenant assigned to the context must be valid: itsIdmust be non-default and itsIdentifiernon-empty (seeITenantInfoandTenantInfo). - The
IsResolvedproperty indicates whether a tenant has been resolved (TenantInfois not null). - Can be obtained in ASP.NET Core from the current request's
HttpContextwithGetTenantContext<TTenantInfo, TId>()or theTenantContext<TId>()extension. See ASP.NET Core Integration for details. - The
HttpContextextension methodSetTenantInfocan be used to manually set the current tenant, but normally the middleware handles this. UseTrySetTenantInfoif you need to set only when no tenant has been resolved yet. - A custom implementation can be defined for advanced use cases.
MultiTenant Strategies
Responsible for determining and returning a tenant identifier string for the current request.
- Several strategies are provided based on host, route, etc. See MultiTenant Strategies for more information.
- Custom strategies implementing
IMultiTenantStrategycan be used as well.
MultiTenant Stores
Responsible for returning a TenantInfo object based on a tenant string identifier (which is usually provided by a
strategy).
- Has methods for adding, removing, updating, and retrieving
TenantInfoobjects. - Several implementations are provided, including a basic lock-protected
InMemoryStoreand a more advanced Entity Framework Core based implementation. - Custom stores implementing
IMultiTenantStorecan be used as well.
MultiTenantException
Exception type thrown when a serious problem occurs within MultiTenant.
- Usually wraps an underlying exception.
See Also
- Configuration and Usage — service registration and resolution
- Getting Started — quick start walkthrough
- ASP.NET Core Integration — ASP.NET Core-specific details
- .NET Generic Host Integration — non-web scenarios
- MultiTenant Strategies — all built-in strategies
- MultiTenant Stores — all built-in stores