Sparkdo.MultiTenancy 2.0.0-preview.2

This is a prerelease version of Sparkdo.MultiTenancy.
dotnet add package Sparkdo.MultiTenancy --version 2.0.0-preview.2
                    
NuGet\Install-Package Sparkdo.MultiTenancy -Version 2.0.0-preview.2
                    
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="Sparkdo.MultiTenancy" Version="2.0.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.MultiTenancy" Version="2.0.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.MultiTenancy" />
                    
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 Sparkdo.MultiTenancy --version 2.0.0-preview.2
                    
#r "nuget: Sparkdo.MultiTenancy, 2.0.0-preview.2"
                    
#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 Sparkdo.MultiTenancy@2.0.0-preview.2
                    
#: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=Sparkdo.MultiTenancy&version=2.0.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.MultiTenancy&version=2.0.0-preview.2&prerelease
                    
Install as a Cake Tool

Sparkdo.MultiTenancy

Sparkdo.MultiTenancy 是无宿主依赖的多租户运行时。它实现当前租户上下文、租户解析管线、租户目录、显式租户执行作用域,以及基于 Manifest 和 IServiceCollection 的默认接线。

安装

dotnet add package Sparkdo.MultiTenancy

只消费契约时引用 Sparkdo.MultiTenancy.Abstractions。在普通 Generic Host 中可以直接调用:

services.AddSparkdoMultiTenancy(options =>
{
    options.IsEnabled = true;
});

Sparkdo 应用会通过 SparkdoMultiTenancyManifest 自动接入同一组默认服务。

当前租户

ICurrentTenant 使用 AsyncLocal 保存异步流上下文,嵌套作用域会在释放时恢复上层租户:

using (currentTenant.Change("tenant-a", "tenant-a"))
{
    await service.ExecuteAsync(cancellationToken);
}

不要把租户标识放入静态字段,也不要跨请求共享可变租户对象。

解析管线

通过 SparkdoTenantResolveOptions.TenantResolvers 按顺序加入贡献者。贡献者可以设置 TenantIdOrName 选择租户,也可以设置 Handled = true 明确选择宿主上下文;第一个完成解析的贡献者会短路后续贡献者。没有贡献者处理时才使用 FallbackTenant

解析上下文包含独立的 Items 字典,HTTP、消息、作业等适配包可以在不污染核心契约的前提下传递宿主数据。

显式执行语义

ITenantExecution 把常见意图表达成独立入口:

  • ForTenantAsync:以租户身份执行普通业务。
  • ForHostAsync:以宿主身份执行。
  • ForTenantDataAsync:宿主代访问指定租户数据,ambient 租户保持宿主。
  • CrossTenantReadAsync:跨租户只读聚合,ambient 租户保持宿主。
  • ForEachTenantAsync:按租户目录顺序处理所有活动租户,并尊重取消。

核心包只负责上下文和语义,不执行权限检查、不切换数据库连接、不引用 EF Core、ASP.NET Core、认证或消息基础设施。数据隔离和连接解析应由独立适配包组合 TenantExecutionContextIMultiTenant

默认租户目录

DefaultTenantStoreSparkdoDefaultTenantStoreOptions 构建不可变索引,并订阅 IOptionsMonitor 的变更。查询按租户标识、真实名称或显式标准化名称进行;每次刷新都会复制连接字符串,避免外部配置对象被修改后污染运行时快照。

Product Compatible and additional computed target framework versions.
.NET 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 (6)

Showing the top 5 NuGet packages that depend on Sparkdo.MultiTenancy:

Package Downloads
Sparkdo.Ddd.Domain

Sparkdo DDD 领域模型库,提供领域驱动设计的核心组件实现

Sparkdo.EntityFrameworkCore

Sparkdo Entity Framework Core 的多租户 DbContext 生命周期和工作单元集成。

Sparkdo.TenantManagement.Domain

Sparkdo 租户管理领域层,提供租户管理的核心业务逻辑和数据访问接口

Sparkdo.Users.Abstractions

Sparkdo 用户管理抽象库,提供用户管理功能的核心接口定义和事件定义

Sparkdo.UI.Navigation

library for the Sparkdo framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-preview.2 54 7/20/2026
1.0.4-preview.3 208 6/8/2026
1.0.4-preview.2 206 6/7/2026
1.0.4-preview.1 202 6/7/2026
1.0.3 473 6/6/2026
1.0.3-preview.1 212 5/17/2026
1.0.2 381 4/27/2026
1.0.2-preview.5 105 4/15/2026
1.0.2-preview.4 116 2/8/2026
1.0.2-preview.3 122 2/1/2026
1.0.2-preview.2 143 1/31/2026
1.0.2-preview.1 210 12/4/2025
1.0.1 1,157 11/27/2025
1.0.0 1,134 11/25/2025
1.0.0-preview.5 155 10/24/2025