![](/img/trans.png)
[英]appsettings.Production.json has values from appsettings.json afte publishing
[英]Use Application settings from Azure Web App directly, not from appsettings.Production.json
我想使用Azure Web App中定義的生產環境的應用程序設置: 請參閱此處
目前使用appsettings*.json
文件在Program.cs
獲取這些值以獲得正確的環境:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
IHostingEnvironment env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>()
.Build();
這些值目前在json中定義,例如
"ConnectionStrings": {
"LocalizationAdminContext": "Data Source=example;Initial Catalog=example;Integrated Security=True;MultipleActiveResultSets=True"
},
"DbResourceConfiguration": {
"ConnectionString": "Data Source=example;Initial Catalog=example;Integrated Security=True;MultipleActiveResultSets=True"
}
我試圖從appsettings.production.json
刪除它們以強制它使用Azure中提供的那些,但它是錯誤的。 我認為,因為它在Program.cs
設置,它將始終在相關環境json文件中搜索設置。
如何在配置中覆蓋此選項以使用應用程序設置? 優選僅適用於生產環境。
好吧,我不確定這是否真的是問題,但無論如何它都是無用的代碼。 除其他外, CreateDefaultBuilder
配置各種配置提供程序,包括appsettings.json
和appsettings.{environment}.json
。 然后它繼續添加環境變量之類的東西。
在最好的情況下,沒有理由調用ConfigureAppConfiguration
,因為這些文件已經通過調用CreateDefaultBuilder
添加。 但是,還有一個操作順序如何添加配置:即稍后添加的配置提供程序將覆蓋之前添加的配置提供程序。 因此,通過像這樣再次添加JSON文件,您實際上將它們作為包含的最后一個配置提供程序,這意味着它們會覆蓋所有內容 。 我相信,Azure中的應用程序設置是作為環境變量添加的。 這通常會使它們覆蓋您的JSON文件,因為環境變量提供程序是在JSON提供程序之后添加的。 但是,您現在已經翻轉了腳本,使它們被JSON覆蓋。
我不確定為什么從JSON中刪除它們會導致錯誤,但我有理由相信,只需刪除ConfigureAppConfiguration
調用就可以使一切按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.