Docs
Tenantry CoreAPI reference

TenantryServiceCollectionExtensions class

Namespace: Microsoft.Extensions.DependencyInjection · Package: Tenantry.Core · API reference

Registers Tenantry.

public static class TenantryServiceCollectionExtensions

Methods

AddTenantry<TKey>(IServiceCollection, Action<ITenantBuilder<TKey>>?)

Registers Tenantry for tenant keys of type TKey, then calls configure to add its features: a tenant store, how requests are resolved to tenants, connection strings, EF Core isolation and so on.

public static IServiceCollection AddTenantry<TKey>(this IServiceCollection services, Action<ITenantBuilder<TKey>>? configure = null) where TKey : IEquatable<TKey>, IParsable<TKey>

Type parameters:

  • TKey: The tenant identifier type (e.g. Guid, int, string). Must implement IEquatable<T> and IParsable<TSelf>.

Parameters:

  • services IServiceCollection: The application's service collection.
  • configure Action<ITenantBuilder<TKey>>: Adds Tenantry's features, or null to register only the core services.

Returns: IServiceCollection: The same services for chaining.

Exceptions:

  • InvalidOperationException: Tenantry is already registered with another tenant key type.

The core services are the ambient tenant (ITenantContext<TKey> and ITenantContextSetter<TKey>), ITenantScopeFactory<TKey>, ITenantLookup<TKey> and ITenantStoreCache<TKey>, all singletons. They serve web applications, workers and console tools alike; the ASP.NET Core features come from the Tenantry.AspNetCore package.

Calling it again adds to the same registration, so a library can call it to make sure Tenantry is registered. An application uses one tenant key type: calling it with another throws.

builder.Services.AddTenantry<Guid>(tenant => tenant
    .ResolveFromHeader("X-Tenant-Id")
    .UseStore<AppTenantStore>());

On this page