Table of Contents

Class TenantContextHolder

Namespace
Excalibur.Dispatch
Assembly
Excalibur.Dispatch.Abstractions.dll

Ambient, async-flow-local store for the current tenant id. A tenant is established for a logical operation with BeginScope(string?) (a using scope); ITenantContext reads it without a setter, so the ambient tenant is structurally read-only to consumers.

public static class TenantContextHolder
Inheritance
TenantContextHolder
Inherited Members

Properties

AmbientContext

Gets an ITenantContext view over the ambient tenant, for a caller that needs the context type rather than the raw identifier — a store that resolves its partition from a context, or a conformance suite that varies the ambient scope around one store instance.

public static ITenantContext AmbientContext { get; }

Property Value

ITenantContext

A shared, stateless reader. It holds no state of its own: every read resolves whatever scope BeginScope(string?) has established on the calling async flow at that moment, so one instance is correct for every tenant and is safe to hold as a singleton.

Remarks

Outside any scope this resolves the reserved untenanted partition, never null. "No tenant was established" is a partition this framework names (UntenantedSentinel), not an undecided state, and reads and writes there are confined to it exactly as a real tenant's are. HasTenant is therefore always true: a caller is always addressing some partition.

Resolving null instead would mean "no decision was made", which a store that requires a tenant refuses outright. A reader written as a bare Current read therefore fails every operation that runs outside a scope, not only the tenant-scoped ones — which is why this obligation is stated here rather than left to each caller to rediscover.

A host that wants that refusal wants the registered context instead: AddTenantContext() registers one that leaves an unresolved tenant unresolved, so a tenant-required store fails closed rather than writing to the untenanted partition. Use this property where the untenanted partition is a legitimate destination, not to silence that guard.

Current

Gets the current ambient tenant id, or null when none is established.

public static string? Current { get; }

Property Value

string

The ambient tenant id, or null.

Methods

BeginScope(string?)

Establishes tenantId as the ambient tenant for the current async flow until the returned scope is disposed, when the previous ambient tenant is restored (scopes nest correctly).

public static IDisposable BeginScope(string? tenantId)

Parameters

tenantId string

The tenant id to make ambient, or null to clear it in-scope.

Returns

IDisposable

A scope that restores the previous ambient tenant on disposal.

Remarks

Over-length is refused here rather than at the point of use. An id no provider can store whole would otherwise establish cleanly, read back as present, and throw later in some store — far from the call that established it and with nothing left to say who did. Refusing at establishment keeps the failure where the caller still has the context to say what they meant.

The other two values the conversion rejects are deliberately accepted here, and this is the distinction worth stating because the symmetric-looking rule is wrong. A blank id resolves to the untenanted partition rather than to a tenant named by whitespace, and the reserved sentinel is a storage encoding a store hands straight back on read — refusing either would turn a legitimate value into a throw. This ingress is not the tenant-naming guard; it establishes an ambient term, and "no tenant" is one of the terms it must be able to carry.

Exceptions

ArgumentException

tenantId is longer than MaxLength characters, which no shipped provider can store whole.