Files
Yi.Admin/Yi.Framework.Net6/src/framework/Yi.Framework.MultiTenancy/Extensions/MultiTenancyExtensions.cs
2023-03-02 10:19:18 +08:00

43 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.Extensions.DependencyInjection;
using Yi.Framework.MultiTenancy.ResolveContributor;
namespace Yi.Framework.MultiTenancy.Extensions;
/// <summary>
/// 租户注入扩展方法
/// </summary>
public static class MultiTenancyExtensions
{
/// <summary>
/// 注入 租户
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddCurrentTenant(this IServiceCollection services)
{
services.Configure<TenantResolveOptions>(option =>
{
//添加租户解析器,默认添加从当前用户中获取
//添加从httpheader解析TenantId配置的租户id
option.TenantResolvers.Add(new HttpHeaderTenantResolveContributor());
//添加配置租户解析器解析TenantId配置的租户id
option.TenantResolvers.Add(new ConfigurationTenantResolveContributor());
});
//添加租户解析器注入
services.AddTransient<ITenantResolver, TenantResolver>();
//添加当前租户
services.AddTransient<ICurrentTenant, CurrentTenant>();
//添加默认访问器
services.AddTransient<ICurrentTenantAccessor, DefaultCurrentTenantAccessor>();
return services;
}
}