我们遇到了与您相同的问题,每个客户只有一个分支机构,但随着我们的客户群开始增长,事实证明这真的很痛苦。我们最终做的是为所有客户(开发、登台、产品)建立一个分支,并创建一个appsettings.json
层次结构:
appsettings.json * config for all customers
appsettings.Development.json * config for all customers, for dev environment
appsettings.Production.json * config for all customers, for prod environment
appsettings.client.json * dummy file, just to have a proper hierarchy in VS
appsettings.client.Customer1.json
appsettings.client.Customer1.Development.json
appsettings.client.Customer1.Production.json
appsettings.client.Customer2.json
appsettings.client.Customer2.Development.json
appsettings.client.Customer2.Production.json
为了为每个客户加载正确的 appsettings,我们使用了一个环境变量,称为(ASPNETCORE_CustomerName
任何前缀ASPNETCORE_
为Program.cs
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
// read the customer name from the env variable
// (note that the ASPNETCORE_ prefix is removed)
var customer = hostingContext.Configuration.GetValue<string>("CustomerName");
// only add our custom hierarchy,
// the default json files are already loaded
config
.AddJsonFile($"appsettings.client.{customer}.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.client.{customer}.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
;
})
.UseStaticWebAssets()
.UseStartup<Startup>();
});
}
}
最后,我们每个客户都有一个 CI/CD 管道,每个客户 Web 应用程序ASPNETCORE_CustomerName
通过 Azure 门户设置自己的变量。