gpt4 book ai didi

c# - ASP.NET Core - 每个租户的 IOptions,每个租户或 Vault 的 appsetting.json

转载 作者:行者123 更新时间:2023-12-04 17:34:41 27 4
gpt4 key购买 nike

我们目前正在将 Web 应用单租户转换为 Multi-Tenancy 。

我们希望根据 TenantId(从 header 、查询字符串或 RabbitMQ header 中检索)覆盖 appsettings.json 中包含的数据。

“租户识别”服务运作良好。我们得到了一个返回 TenantIdITenantResolverService

目前,我们的 IOptions 被 appsettings.json 覆盖,然后是 appsettings.Development.json,最后被环境变量覆盖。

public Startup(IHostingEnvironment environment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", true)
.AddJsonFile("appsettings.local.json", true)
.AddJsonFile($"k8s-mount/appsettings.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

我们希望在 环境变量 之前添加一个基于 TenantId 的覆盖。例如:appsettings.tenantId.json,有时来自 Vault 中的 JSON 文件。

我们应该从哪里开始?我们已经看到很多关于每个租户的“硬编码”值(value)的文章。 (例如:https://michael-mckenna.com/multi-tenant-asp-dot-net-core-application-tenant-specific-configuration-options)

但似乎没有什么能满足要求,

最佳答案

很抱歉没有早点到这里,但是这里是您可以尝试的。使用文件配置提供程序并将您的租户配置放在由租户命名的文件中。 ASP.NET 运行时将为您监视文件。最初构建此机制是为了使用每个配置值的文件,因此如果您想要配置对象,则必须反序列化它们。

这里是添加文件配置提供者的方法:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(confBuilder =>
{
confBuilder.SetBasePath(Directory.GetCurrentDirectory());
var path = Path.Combine(Directory.GetCurrentDirectory(), "tenant-configs");
confBuilder.AddKeyPerFile(path, true);
})
.UseStartup<Startup>();

您可以在这里找到完整的资源:https://github.com/MichaelSL/asp.net-multitenant-configs

关于c# - ASP.NET Core - 每个租户的 IOptions,每个租户或 Vault 的 appsetting.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152075/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com