MultiTenant Docs

Guides and reference for Finbuckle MultiTenant, versioned to match the packages you install.

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.

  • Id is 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/whitespace string — is treated as no tenant and is rejected (a MultiTenantException is thrown when adding, updating, resolving, or assigning such a tenant, and an ArgumentException when looking one up by a default id).
  • Identifier is 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. Unlike Id, Identifier can 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 TenantInfo included a connection string property. If needed simply add it to your custom TenantInfo derived 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:

  • Id never changes. It is the tenant's identity; tenant equality is Id equality.
  • Identifier may change, but only through the store. Create a new instance carrying the new Identifier and pass it to TenantManager<TTenantInfo, TId>.UpdateAsync; the store re-keys its lookups and hands out the new instance from then on. Never change Identifier in 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. Whether Identifier matching is case-sensitive depends on the store: InMemoryStore and ConfigurationStore are 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/Identifier values 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 (ConfigurationStore and EchoStore) 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 calling BeginScope() / BeginTenantScope().
  • You establish a scope with ITenantScopeProvider.BeginScope() or, more commonly, the IServiceProvider.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.HasScope reports whether a scope is active. ASP.NET Core ends the scope automatically when the request's HttpContext is disposed.
  • If you replace the default registration, register ITenantScopeProvider as a singleton that shares its ambient state with ITenantContext, as the default AmbientTenantContext registration does. In ASP.NET Core, UseMultiTenant<TId>() throws a MultiTenantException at startup if it resolves to different instances.
  • Accessing TenantInfo before a scope has been established throws a MultiTenantException ("No ambient tenant scope has been established.").
  • The TenantInfo property holds the current tenant info. Within a scope it can only be set once — attempting to set it a second time throws a MultiTenantException. A tenant assigned to the context must be valid: its Id must be non-default and its Identifier non-empty (see ITenantInfo and TenantInfo).
  • The IsResolved property indicates whether a tenant has been resolved (TenantInfo is not null).
  • Can be obtained in ASP.NET Core from the current request's HttpContext with GetTenantContext<TTenantInfo, TId>() or the TenantContext<TId>() extension. See ASP.NET Core Integration for details.
  • The HttpContext extension method SetTenantInfo can be used to manually set the current tenant, but normally the middleware handles this. Use TrySetTenantInfo if 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 IMultiTenantStrategy can 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 TenantInfo objects.
  • Several implementations are provided, including a basic lock-protected InMemoryStore and a more advanced Entity Framework Core based implementation.
  • Custom stores implementing IMultiTenantStore can be used as well.

MultiTenantException

Exception type thrown when a serious problem occurs within MultiTenant.

  • Usually wraps an underlying exception.

See Also