ServiceCollectionExtensions class
Namespace: Tenantry.Core.Extensions · Package: Tenantry.Core · API reference
Extension methods for registering core Tenantry services.
public static class ServiceCollectionExtensionsMethods
AddTenantryCore<TKey>(IServiceCollection, Action<ITenantBuilder<TKey>>?)
Registers the core Tenantry services: the tenant context accessor (AsyncLocal singleton) behind ITenantContext<TKey> and ITenantScope<TKey>, plus the ITenantScopeFactory<TKey> and ITenantStoreAccessor<TKey> singletons used by background work.
public static IServiceCollection AddTenantryCore<TKey>(this IServiceCollection services, Action<ITenantBuilder<TKey>>? configure = null) where TKey : IEquatable<TKey>, IParsable<TKey>Type parameters:
TKey: The tenant identifier type.
Parameters:
servicesIServiceCollection: The application's service collection.configureAction<ITenantBuilder<TKey>>: Configures the tenant store and other Core services, or null to register only the defaults.
Returns: IServiceCollection
Use this entry point for worker services, console apps, and other non-HTTP hosts. For ASP.NET Core applications, use AddTenantry<TKey>() instead, which calls this method internally and adds HTTP-specific resolution on top.
All registrations are idempotent — calling both AddTenantryCore and AddTenantry is safe.
// Worker service — no ASP.NET Core required
builder.Services.AddTenantryCore<Guid>(tenant =>
{
tenant.AddEfCoreIsolation(options => options.OnMissingTenant = MissingTenantBehavior.Reject);
});
// Run EF Core work as a tenant, in its own DI scope: var scopes = sp.GetRequiredService<ITenantScopeFactory<Guid>>(); await using (var scope = scopes.CreateScope(tenant)) { var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); // reads are filtered to the tenant and writes are stamped with it } ```ConnectionStringExtensions class
Registers per-tenant connection strings: TenantConnectionStringOptions<TKey>tenantry-core-tenantconnectionstringoptions.md and ITenantConnectionStringResolver<TKey>tenantry-core-itenantconnectionstringresolver.md.
InMemoryTenantStore<TKey> class
An ITenantStore<TKey>tenantry-core-itenantstore.md backed by an in-memory dictionary.