小编I3e*_*ers的帖子

.net Core 在配置不同的配置提供程序时访问 appsettings.json

我刚刚开始使用 .net Core 2.1,我编写了一个自定义 ConfigurationProvider,它从一个单独的文件加载我的敏感配置。这适用于硬编码路径,但我也希望将文件的位置存储在配置中。如果我在我的提供者设置期间尝试访问 appsettings 配置,则值不存在。想必这是因为我还在配置阶段。有没有办法访问它们或失败,有没有办法在 Startup.cs 而不是 Program.cs 中添加另一个配置提供程序?

这就是我想要做的:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        logging.ClearProviders();
                        logging.AddConsole();
                        logging.AddFile(opts =>
                        {
                            hostingContext.Configuration.GetSection("FileLoggerOptions").Bind(opts);
                        });
                    })
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
                        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                        config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true);
                        config.AddEncryptedFile(hostingContext.Configuration["SecureSettingsFile"], true, true);
                    })
                    .UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我可以访问日志配置中的配置,但不能访问应用程序配置。我的提供者是AddEncryptedFile位,hostingContext.Configuration["SecureSettingsFile"]应该从 appsettings.json 读取值。

此阶段唯一可用的配置是环境变量,我真的不想使用它们,因为服务器上有大约 30 个网站。

我正在尝试做的事情是否可行,或者我是否必须使用环境变量或将我的加密配置存储在与我的网站相同的位置?

谢谢

c# asp.net-core-2.1

3
推荐指数
1
解决办法
1201
查看次数

标签 统计

asp.net-core-2.1 ×1

c# ×1