小编Ele*_*lin的帖子

我可以在 ASP.NET Core MVC 应用程序中将配置从 appsettings.json 移动到数据库吗?

我有这个应用程序,它使用 来appsettings.json存储一些配置,例如 API 地址、令牌、保存文件的路径等。

我们没有 DevOps,因此对于我们在应用程序设置中所做的每项更改,我们需要要求基础架构团队将更改部署到生产中。

也就是说,我在想是否可以将一些配置放入数据库和Startup构造函数中,或者ConfigureServices从数据库中获取此信息并在应用程序中使用它。

例如,每次我向应用程序设置添加新密钥时,都不需要进行新的部署。我只需在数据库中创建一条新记录,应用程序将使用此配置,就像它在应用程序设置中一样。

今天我们有这个:

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();
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可行的或可能的类似的事情:

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();
}
Run Code Online (Sandbox Code Playgroud)

是否可以?或者我正在尝试重新发明轮子?

预先感谢您的宝贵时间!

database asp.net-mvc configuration-files appsettings asp.net-core-2.1

14
推荐指数
1
解决办法
7119
查看次数