代码之家  ›  专栏  ›  技术社区  ›  Paul Farry

如何从最低级别值读取配置值

  •  0
  • Paul Farry  · 技术社区  · 8 年前

    我试图为我的应用程序提供一些配置数据,以便在源代码管理中存储简单的设置,但我们的部署系统将替换中的加密密钥appsettings.json文件当它展开时。

    对于开发,我仍然需要一个通用密钥,但是对于特定用户的秘密,我想提供一个他们可以确保安全的值。

    我的配置文件设置如下:

    {
      "SystemConfiguration" : {
        "EncryptionKey" :  "weak"
      }
    }
    

    appsettings.Development.json文件

    {
      "SystemConfiguration" : {
        "EncryptionKey" :  "devweak"
      }
    }
    

    {
      "SystemConfiguration" : {
        "EncryptionKey" :  "this is a secret"
      }
    }
    

    在我的控制器结构中,我要注射 IConfiguration configuration

     public SysConfigController(IConfiguration configuration)
     {
         var result = configuration["SystemConfiguration:EncryptionKey"];
     }
    

    但是result的值总是“弱”的,除非更高级别的设置文件根本不包含该值( : null )也不管用。

    有没有一种方法可以检索最低级别的值?

    2 回复  |  直到 8 年前
        1
  •  1
  •   Tseng    8 年前

    似乎你的配置注册是错误的。将使用包含特定密钥的最后一次注册。

    例如在你的 Program.cs Startup.cs 的构造函数ASP.NETCore1.x)您可以覆盖注册(或者在使用默认host builder方法时只添加您喜欢的注册):

    public static IWebHostBuilder BuildWebHost(string[] args) =>
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;
    
                config
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                    .AddUserSecrets<Startup>()
                    .AddEnvironmentVariables()
                    .AddCommandLine(args);
            })
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights();
    

    • .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) : 全局配置文件
    • .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) appsettings.Development.json
    • .AddUserSecrets<Startup>() 用户机密
    • .AddEnvironmentVariables() 环境变量
    • .AddCommandLine(args); 命令行参数

    查找键时,搜索按相反顺序进行。如果将其定义为命令行参数,它将覆盖所有其他键和值(来自环境变量、用户机密、环境特定和全局文件)。

    所以把最不重要的文件放在开头,最重要的(覆盖的)文件放在结尾。

    在这种情况下,如果在用户机密中定义了它,它将覆盖 appsettings.json 提供的值。

    the docs

    按照启动时指定配置提供程序的顺序读取配置源。本主题中描述的配置提供程序是按字母顺序描述的,而不是按代码的排列顺序。在代码中排序配置提供程序,以适合底层配置源的优先级。

    配置提供程序的典型序列是:

    • 文件(appsettings.json文件,appsettings..json,其中是应用程序的当前宿主环境)
    • 环境变量

    通常的做法是将命令行配置提供程序放在一系列提供程序的最后,以允许命令行参数覆盖其他提供程序设置的配置。

        2
  •  0
  •   lails    8 年前

    ConfigureServices(IServiceCollection services) 在启动.cs.

    services.Configure<SystemConfiguration>(options => Configuration.GetSection("SystemConfiguration").Bind(options));
    
    public class SystemConfiguration
    {
       public string EncryptionKey{get;set;}
    }
    

    然后,您可以使用DI获取类

    public class SomeClass
    {
       private readonly SystemConfiguration _systemConfiguration{get;set;}
       public SomeClass (IOptions<ConfigExternalService> systemConfiguration)
       {
        _systemConfiguration = systemConfiguration;
       }
    }