代码之家  ›  专栏  ›  技术社区  ›  Bin4ry

子文件夹中的应用程序设置

  •  2
  • Bin4ry  · 技术社区  · 6 月前

    我想加载三个不同的应用程序设置。

    • 应用程序参数
    • 应用程序设置。A.json
    • 其他/应用程序设置。A.json

    以便用户可以放置他的应用设置。将A.json放入子文件夹,不干扰deplyments。然而 appsettings.A.json other 文件夹未加载。我尝试了一条完整的路径,一条相对的路径,现在 PhysicalFileProvider 。但是,它总是使用 应用程序设置。A.json 。如果我删除 应用程序设置。A.json 它使用默认值中的回退值 appsettings.json .

    IHostBuilder builder = Host.CreateDefaultBuilder().UseEnvironment("A");
    
    builder.ConfigureHostConfiguration(config =>
    {
        var userConfigProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "other"));
    
        config
            .AddEnvironmentVariables()
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile("appsettings.A.json", false, true)
            .AddJsonFile(userConfigProvider, "appsettings.A.json", false, true);
    });
    
    builder.ConfigureServices((host, services) => services.Configure<Config>(host.Configuration));
    
    IHost host = builder.Build();
    
    Console.WriteLine(host.Services.GetService<IOptions<Config>>()!.Value.File);
    
    Console.ReadLine();
    

    我在这里错过了什么?此外,该文件确实存在,我确实对c进行了健全性检查。

    1 回复  |  直到 6 月前
        1
  •  2
  •   pfx    6 月前

    你需要 ConfigureAppConfiguration 而不是 ConfigureHostConfiguration .

    从文档中:

    结果将于 Configuration 用于后续操作,以及 Services .