ITenantConnectionStringResolver<TKey> interface
Namespace: Tenantry.Core · Package: Tenantry.Core · API reference
Returns tenants' connection strings, as configured by TenantConnectionStringOptions<TKey>.
Registered as a singleton by UseConnectionStrings. The parameterless overloads use the current tenant (from the request, or from a scope opened with ITenantScopeFactory<TKey>); the others take the tenant explicitly, for code such as migration runners that visits tenants without making each one current.
With a regular AddDbContext, resolve in the options callback, which runs for every new context: options.UseSqlServer(sp.GetRequiredService<ITenantConnectionStringResolver<Guid>>().Resolve()). Do not do this with AddDbContextPool or AddPooledDbContextFactory: their options callback runs once, so every pooled context would keep the first tenant's connection string.
public interface ITenantConnectionStringResolver<TKey> where TKey : IEquatable<TKey>, IParsable<TKey>Type parameters
TKey: The tenant identifier type. SeeITenantDescriptor<TKey>for constraints.
Derived types: TenantConnectionStringResolver<TKey>.
Methods
Resolve()
Returns the current tenant's connection string.
string Resolve()Returns: string
Exceptions:
TenantNotResolvedException: No tenant is current.InvalidOperationException: OnlyTenantConnectionStringOptions<TKey>.GetConnectionStringAsyncis configured (useITenantConnectionStringResolver<TKey>.ResolveAsync), or the delegate returned an empty value.
Resolve(ITenantDescriptor<TKey>)
Returns tenant's connection string.
string Resolve(ITenantDescriptor<TKey> tenant)Parameters:
tenantITenantDescriptor<TKey>: The tenant whose connection string to return.
Returns: string
Exceptions:
InvalidOperationException: OnlyTenantConnectionStringOptions<TKey>.GetConnectionStringAsyncis configured (useITenantConnectionStringResolver<TKey>.ResolveAsync), or the delegate returned an empty value.
ResolveAsync(CancellationToken)
Returns the current tenant's connection string, using the asynchronous delegate if configured.
ValueTask<string> ResolveAsync(CancellationToken cancellationToken = default)Parameters:
cancellationTokenCancellationToken: Cancels the lookup.
Returns: ValueTask<string>
Exceptions:
TenantNotResolvedException: No tenant is current.InvalidOperationException: The delegate returned an empty value.
ResolveAsync(ITenantDescriptor<TKey>, CancellationToken)
Returns tenant's connection string, using the asynchronous delegate if configured.
ValueTask<string> ResolveAsync(ITenantDescriptor<TKey> tenant, CancellationToken cancellationToken = default)Parameters:
tenantITenantDescriptor<TKey>: The tenant whose connection string to return.cancellationTokenCancellationToken: Cancels the lookup.
Returns: ValueTask<string>
Exceptions:
InvalidOperationException: The delegate returned an empty value.