LinKit.Tenancy
2.0.1
dotnet add package LinKit.Tenancy --version 2.0.1
NuGet\Install-Package LinKit.Tenancy -Version 2.0.1
<PackageReference Include="LinKit.Tenancy" Version="2.0.1" />
<PackageVersion Include="LinKit.Tenancy" Version="2.0.1" />
<PackageReference Include="LinKit.Tenancy" />
paket add LinKit.Tenancy --version 2.0.1
#r "nuget: LinKit.Tenancy, 2.0.1"
#:package LinKit.Tenancy@2.0.1
#addin nuget:?package=LinKit.Tenancy&version=2.0.1
#tool nuget:?package=LinKit.Tenancy&version=2.0.1
LinKit.Tenancy
LinKit.Tenancy provides an asynchronous-flow tenant context and helpers to explicitly place/read a tenant ID in LinKit message headers. It does not automatically resolve HTTP identity, authorize a tenant, or filter database queries.
Install and register
dotnet add package LinKit.Tenancy
builder.Services.AddLinKitTenancy();
Inject ITenantAccessor (or ITenantContext) into scoped application services:
public sealed class TenantOrderService(ITenantAccessor tenants, IOrderRepository orders)
{
public Task<IReadOnlyList<Order>> GetOrdersAsync(CancellationToken cancellationToken)
{
// Require throws when no scope is active; TryGet is the opt-out form when a tenant is optional.
var tenantId = tenants.Require().TenantId
?? throw new InvalidOperationException("A tenant must be resolved before querying orders.");
return orders.GetForTenantAsync(tenantId, cancellationToken);
}
}
The application must validate the tenant ID and enforce isolation in authorization and data-access layers. Merely setting TenantId does not add a database filter.
Resolve and clear tenant context
Nothing is created implicitly: outside a scope ITenantAccessor.Current is null, TryGet returns false, and Require() throws. Resolve the tenant at the request or message-processing boundary and wrap the work in a scope — disposing it restores whatever tenant was ambient before (usually null):
using (tenantAccessor.BeginScope(validatedTenantId))
{
await next();
}
Assert.Null(tenantAccessor.Current); // guaranteed reset, even when `next()` throws
Scopes nest: an inner scope restores the outer tenant when it is disposed. TenantAccessor uses AsyncLocal so the tenant follows the asynchronous execution flow. Avoid passing tenant-bound work into detached/background tasks without explicitly starting a scope there.
For outbound calls, TenantPropagationHandler is a DelegatingHandler that adds the x-tenant-id header and re-establishes the ambient tenant for the duration of the request:
services.AddHttpClient<InventoryClient>(client => client.BaseAddress = inventoryUri)
.AddHttpMessageHandler<TenantPropagationHandler>();
Propagate through message headers
var headers = new MessageHeaders();
TenantPropagation.Inject(headers, tenantAccessor.Current?.TenantId);
var incomingTenantId = TenantPropagation.Extract(headers);
Generated consumers call every registered IMessageConsumeInterceptor per message. AddLinKitTenancy() registers TenantConsumeInterceptor, which sets the tenant from linkit-tenant-id for that message and restores the previous context afterwards. The header is trusted broker metadata, so only allow trusted publishers on the queue. Tenant data filtering still must be enforced by your data layer.
OutboxMessage.Create(...) captures the ambient tenant into the same header, and OutboxPublisherService restores it before handing the message to the broker, so the tenant survives the transaction boundary without any wiring.
The message header key is linkit-tenant-id (MessageHeaderNames.TenantId). TenantHeaderName (x-tenant-id) is the separate HTTP naming constant.
License
MIT. See the repository license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- LinKit.Core (>= 4.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
-
net8.0
- LinKit.Core (>= 4.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
-
net9.0
- LinKit.Core (>= 4.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LinKit.Tenancy:
| Package | Downloads |
|---|---|
|
LinKit.Outbox
Transactional Outbox and Inbox abstractions with an explicit message lifecycle and replaceable store implementations for LinKit. |
GitHub repositories
This package is not used by any popular GitHub repositories.
v2.0.1: documentation and packaging patch - every public member now carries XML documentation (CS1591 is enforced as a build error) and the README was reviewed for accuracy. Previous release: v2.0.0 - the ambient tenant is no longer auto-created. TryGet/Require are separate and ITenantScope guarantees a deterministic reset. Tenant id is propagated through outbox message headers. Full documentation & release notes: https://quoclinh.id.vn/docs/linkit