LinKit.Tenancy 2.0.1

dotnet add package LinKit.Tenancy --version 2.0.1
                    
NuGet\Install-Package LinKit.Tenancy -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LinKit.Tenancy" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LinKit.Tenancy" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="LinKit.Tenancy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LinKit.Tenancy --version 2.0.1
                    
#r "nuget: LinKit.Tenancy, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LinKit.Tenancy@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LinKit.Tenancy&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=LinKit.Tenancy&version=2.0.1
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
2.0.1 119 11/8/2026
1.2.1 107 10/31/2026
1.2.0 93 10/30/2026
1.1.0 99 10/22/2026
1.0.0 102 10/20/2026

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