Sparkdo.AspNetCore.MultiTenancy 1.0.1

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

Sparkdo.AspNetCore.MultiTenancy

Sparkdo ASP.NET Core 多租户中间件库,提供基于中间件的多租户解析和管理功能。

功能特性

  • 基于 ASP.NET Core 中间件的多租户支持
  • 多种租户解析策略(查询字符串、路由、HTTP头、Cookie、域名)
  • 自动设置当前租户上下文
  • 租户特定的区域文化支持
  • 完善的错误处理机制
  • 与 Sparkdo 框架无缝集成

安装

<PackageReference Include="Sparkdo.AspNetCore.MultiTenancy" Version="x.x.x" />

核心组件

MultiTenancyMiddleware

多租户中间件,负责在请求处理管道中解析和设置当前租户信息:

  • InvokeAsync(): 执行中间件逻辑,解析当前租户并设置相关上下文信息
  • 自动处理租户解析失败情况
  • 支持租户特定的区域文化设置

SparkdoAspNetCoreMultiTenancyModule

多租户模块配置类,负责注册多租户相关的服务和配置:

  • 注册多租户中间件
  • 配置默认的租户解析器
  • 替换默认的租户解析结果访问器

SparkdoAspNetCoreMultiTenancyOptions

多租户配置选项类,用于配置多租户相关的设置:

  • TenantKey: 租户键名称
  • MultiTenancyMiddlewareErrorPageBuilder: 错误页面构建器

租户解析贡献者

项目实现了多种租户解析策略:

  • QueryStringTenantResolveContributor: 从查询字符串中解析租户
  • RouteTenantResolveContributor: 从路由参数中解析租户
  • HeaderTenantResolveContributor: 从 HTTP 头中解析租户
  • CookieTenantResolveContributor: 从 Cookie 中解析租户
  • DomainTenantResolveContributor: 从域名中解析租户

使用方法

1. 在 Startup.Configure 中注册中间件

public void Configure(IApplicationBuilder app)
{
    app.UseMultiTenancy();
    // 其他中间件...
}

2. 配置租户解析选项(可选)

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<SparkdoAspNetCoreMultiTenancyOptions>(options =>
    {
        options.TenantKey = "tenant"; // 默认为 "__tenant"
    });
}

扩展性

该库提供了完整的多租户功能实现,可以通过以下方式扩展:

  1. 实现自定义的 ITenantResolveContributor 来添加特定的租户解析逻辑
  2. 通过 SparkdoAspNetCoreMultiTenancyOptions 配置自定义错误处理逻辑
  3. 实现自定义的 ITenantStore 来提供租户数据存储

代码结构说明

本项目包含以下主要组件:

核心实现

  • MultiTenancyMiddleware: 多租户中间件实现
  • SparkdoAspNetCoreMultiTenancyModule: 多租户模块配置
  • SparkdoAspNetCoreMultiTenancyOptions: 多租户配置选项

租户解析贡献者

  • QueryStringTenantResolveContributor: 查询字符串租户解析贡献者
  • RouteTenantResolveContributor: 路由租户解析贡献者
  • HeaderTenantResolveContributor: HTTP头租户解析贡献者
  • CookieTenantResolveContributor: Cookie租户解析贡献者
  • DomainTenantResolveContributor: 域名租户解析贡献者
  • HttpTenantResolveContributorBase: HTTP租户解析贡献者基类

辅助类

  • HttpContextTenantResolveResultAccessor: HTTP上下文租户解析结果访问器
  • SparkdoMultiTenancyCookieHelper: 多租户Cookie帮助类
  • TenantResolveContextExtensions: 租户解析上下文扩展方法

错误处理

  • MultiTenancyMiddlewareErrorPage: 多租户中间件错误页面
  • MultiTenancyMiddlewareErrorPageModel: 多租户中间件错误页面模型

所有公共接口和类都添加了详细的 XML 文档注释,便于开发者理解和使用。

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 (2)

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

Package Downloads
Sparkdo.AspNetCore.Serilog

Sparkdo ASP.NET Core Serilog 集成库,提供与 Serilog 的无缝集成,支持在日志中添加租户、用户、客户端和关联ID等上下文信息

MYDev.AspNetCore.GraphQL

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2-preview.1 154 12/4/2025
1.0.1 147 11/27/2025
1.0.0 142 11/25/2025
1.0.0-preview.5 116 10/24/2025