[英]can't override appsettings.json settings with environment variables
我無法使用環境變量覆蓋appsettings.json
文件的設置。
appsettings.json
:
{
"AppSettings": {
"LocalUpdatesDir": "<some path>",
"BinaryDeltaCount": 5,
"BinaryDeltaFilenameTemplate": "<template>",
"Azure": {
"User": "user here",
"Password": "password here"
}
},
}
主要的:
public static void Main(string[] args)
{
var webHost = 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, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.UseStartup<Startup>()
.Build();
webHost.Run();
}
環境變量:
更新 1:
在這里我可以看到,我所有的提供者都已注冊:
真正奇怪的是:在環境變量列表中,大約有 80 個條目。 我的兩個新變量不見了,但其中有 2 個環境變量,這是我幾個小時前創建並立即刪除的。 他們到底是從哪里來的?!
更新 2:
我重新啟動了我的計算機,現在我在列表中看到了我的環境變量,但它沒有覆蓋appsettings.json
的值?
從ASPNETCORE_
變量中刪除ASPNETCORE_
前綴或將其作為參數添加到AddEnvironmentVariables
,默認情況下沒有前綴。
編輯:嘗試枚舉配置以查看鍵是否按預期排列。
private static void ShowConfig(IConfiguration config)
{
foreach (var pair in config.GetChildren())
{
Console.WriteLine($"{pair.Path} - {pair.Value}");
ShowConfig(pair);
}
}
我和你在同一條船上,我想出了一些可能對你和其他有同樣頭痛的人有所幫助的事情。
首先,如另一個答案所述,您不需要任何前綴。 所以 ASPNETCORE_ 和 AppSettings 都是不必要的,除非你將它們作為前綴傳遞給 AddEnvironmentVariables()。 所以簡單地去 azure:password。
其次,您不一定需要雙下划線來制作映射嵌套 appsettings 值的 env var。 只用一個冒號。
示例:
應用設置中的這個:
"azure": {
"password": "my.weak.azure.password"
}
可以被這個環境變量覆蓋:
最后,添加到 Windows 環境變量的值顯然是由 Visual Studio 或其后台進程(如控制台窗口主機)緩存的。 關閉並重新打開 Visual Studio 將為您提供新值。 這就是為什么您在重新啟動機器后注意到它們的原因。
如果您從 Visual Studio 使用 .NET 5 創建一個新的 ASP.NET Core Web 應用程序,您的Program.cs
將如下所示:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
查看Host.CreateDefaultBuilder(args)
我們可以看到以下注釋:
從環境變量加載應用程序 Microsoft.Extensions.Configuration.IConfiguration
這意味着如果您使用默認模板覆蓋將起作用。
如果你想用ASPNETCORE_
或DOTNET_
作為前綴,它也可以默認工作。
如果你想使用自定義前綴,你可以這樣做:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables("ASPNETCORECUSTOM_");
});
如果您想使用以下Error
覆蓋Microsoft
,則覆蓋應如下所示:Logging:LogLevel:Microsoft - Error
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
另一種覆蓋 Configuration 的方法是使用ConfigurationInjection
和托管啟動,您可以在其中指定是希望托管啟動的配置優先還是應用程序的配置優先:
public class ConfigurationInjection : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
Dictionary<string, string> dict;
builder.ConfigureAppConfiguration(config =>
{
dict = new Dictionary<string, string>
{
{"ConfigurationKey1",
"From IHostingStartup: Higher priority " +
"than the app's configuration."},
};
config.AddInMemoryCollection(dict);
});
dict = new Dictionary<string, string>
{
{"ConfigurationKey2",
"From IHostingStartup: Lower priority " +
"than the app's configuration."},
};
var builtConfig = new ConfigurationBuilder()
.AddInMemoryCollection(dict)
.Build();
builder.UseConfiguration(builtConfig);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.