Skip to content

TenantScope Class

Runs work of one tenant outside a request resolved to it, for a service that takes no tenant of its own - a security event dispatched from a message consumer, for one.

C#
public sealed class TenantScope : System.IDisposable

Inheritance System.Object → TenantScope

Implements System.IDisposable

Remarks

The tenant accessor answers with the tenant of the innermost scope entered here and, outside any, with the tenant the request was resolved to; entered within a request, a scope takes precedence over it. A service reading the tenant from the accessor therefore serves the tenant entered here, while a service whose method takes the tenant is handed the definition and needs no scope. The scope flows across awaits and ends when disposed:

C#
var tenant = await catalog.FindByIdAsync("acme", cancellationToken)
    ?? throw new InvalidOperationException("No tenant is registered under the id acme.");

using (TenantScope.Enter(tenant))
    await dispatcher.DispatchAsync(descriptor, cancellationToken);

Properties

TenantScope.Current Property

The tenant of the innermost scope entered on this flow of execution, or null outside any.

C#
public static Abblix.Oidc.Server.Features.MultiTenancy.TenantContext? Current { get; }

Property Value

TenantContext

Methods

TenantScope.Dispose() Method

Ends this scope, making current the nearest scope around it that has not ended; a second call changes nothing.

C#
public void Dispose();

Implements Dispose()

Remarks

A scope ended while one entered inside it is still open stays current for that inner scope's work, and is passed over when the inner one ends: restoring its tenant then would run later work as a tenant whose scope is over. It does not throw, since a Dispose that throws while an exception is already leaving a using block would replace that exception.

Work started inside a scope carries the scope with it, and when that work outlives the scope it keeps running as its tenant until it enters and ends a scope of its own; await such work before ending the scope.

TenantScope.Enter(TenantDefinition) Method

Makes tenant the current tenant until the returned scope is disposed.

C#
public static Abblix.Oidc.Server.Features.MultiTenancy.TenantScope Enter(Abblix.Oidc.Server.Features.MultiTenancy.TenantDefinition tenant);

Parameters

tenant TenantDefinition

Returns

TenantScope

Remarks

Call it where the work runs, not inside an async method that returns the scope: the current tenant flows into the work awaited after the call, never back out of an async method to its caller, so a helper that awaits something, enters the scope and returns it leaves its caller with no tenant.