【发布时间】:2020-06-05 09:25:52
【问题描述】:
我有这个应用程序,它使用appsettings.json 来存储一些配置,如 API 地址、令牌、保存文件的路径等。
我们没有 DevOps,因此对于我们在 appsettings 中所做的每项更改,我们都需要要求基础架构团队将更改部署到生产环境中。
也就是说,我正在考虑是否可以将其中的一些配置放入数据库和 Startup 构造函数或 ConfigureServices 以从数据库中获取此信息并在应用程序中使用它。
例如,这只是为了每次我向 appsettings 添加新密钥时都不需要新的部署。我只需在数据库中创建一条新记录,应用程序将使用此配置,就像它在 appsettings 中一样。
今天我们有这个:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
我想知道是否有可行的或可能的类似的东西:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.GetConfigFromDataBase() /// And this will be the method to get from DB the dynamic configuration
.AddEnvironmentVariables();
Configuration = builder.Build();
}
有可能吗?还是我想重新发明轮子?
提前感谢您的宝贵时间!
【问题讨论】:
-
文档中的完美示例可帮助您入门。 Custom configuration provider 您需要进行一些额外的自定义才能获得所有所需的功能,但这应该足以让您继续前进。
-
谢谢@Nkosi,我们将尝试这个解决方案,如果它有效,我会告诉你。
-
感谢@marc_s 的更正。
标签: asp.net-mvc database configuration-files asp.net-core-2.1 appsettings