該項目演示了Microsoft為.NET通用主機應用程序的鍊式配置行為。
在控制台應用中使用Net 6.0和通用主機構建器,並具有“問候”屬性,該屬性在配置中的每個鏈條都被覆蓋。
這是應用每個鍊式配置提供商的順序。最後一個勝利。
有時,了解鍊式配置以及什麼比所需的更大的學習曲線覆蓋。
該演示是為了幫助您理解鍊式配置的順序。
此演示的Azure配置部分使用Azure應用程序配置訪問,目前是一個資源免費的。
如果使用演示的這一部分,則必須在啟動演示之前設置Azure訂閱和環境。
我使用了“問候”可配置屬性,一個帶有標籤,一個沒有標籤。如果要使用這些,或僅使用一個或另一個,則需要將它們添加到Azure應用程序配置中。
標記的一個必鬚根據現有代碼匹配“鍊式配置”。

您還必須在Program.cs中的AddAzureAppConfiguration部分取消分配。
需要熟悉命令行。示例基於Windows的Git Bash,但可以輕鬆地映射到PowerShell或CMD。
這些假設是一個bash終端。轉換為您喜歡的終端/PowerShell/CMD窗口的命令。
這些從Azure App Config倒退,覆蓋了所有以前的設置,以最低的優先級。
從命令行運行時,請清潔(沒有環境變量設置):
隨著Program.cs的azureappconfiguration部分。
~/source/repos/ChainedConfigurationDemo/pub master
└─ cd ..; dotnet publish -o pub --nologo --verbosity quiet; cd pub
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe Greeting=HelloFromArgs
Starting demo...
In the end, the greeting is "HelloFromAzure"
Press Ctrl-C to stop the host and exit.
隨著Program.cs的AzureAppConfiguration部分。

隨著Azure發表評論,並且沒有Dotnet_environment套裝,而不是從Visual Studio的啟動設置中啟動,

~/source/repos/ChainedConfigurationDemo/pub master
└─ cd ..; dotnet publish -o pub --nologo --verbosity quiet; cd pub
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe Greeting=HelloFromArgs
Starting demo...
In the end, the greeting is "HelloFromArgs"
Press Ctrl-C to stop the host and exit.
該應用程序配置為使得如果未設置dotnet_environment或設置為“開發”或“測試”以外的其他東西,則配置構建將屬於appsettings.json(如果屬性存在於appsettings.json)等等,例如 - 例如,如果dotnet_environment to to to to to to to to to to to to to to to to to to to Production”。
Program.cs的AzureAppConfiguration部分。 CS評論 ~/source/repos/ChainedConfigurationDemo/pub master
└─ cd ..; dotnet publish -o pub --nologo --verbosity quiet; cd pub
~/source/repos/ChainedConfigurationDemo/pub master
└─ export DOTNET_ENVIRONMENT=Development
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe
Starting demo...
In the end, the greeting is "HelloFromAppSettings.Development"
Press Ctrl-C to stop the host and exit.
Program.cs的AzureAppConfiguration部分。 CS評論 ~/source/repos/ChainedConfigurationDemo/pub master
└─ cd ..; dotnet publish -o pub --nologo --verbosity quiet; cd pub
~/source/repos/ChainedConfigurationDemo/pub master
└─ export DOTNET_ENVIRONMENT=Test
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe
Starting demo...
In the end, the greeting is "HelloFromAppSettings.Test"
Press Ctrl-C to stop the host and exit.
~/source/repos/ChainedConfigurationDemo/pub master
└─ unset DOTNET_ENVIRONMENT
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe
Starting demo...
In the end, the greeting is "HelloFromAppSettings"
Press Ctrl-C to stop the host and exit.
使用該應用程序的當前配置,這與生產中的結果相同。
~/source/repos/ChainedConfigurationDemo/pub master
└─ export Greeting=HelloFromOperatingEnvironment
~/source/repos/ChainedConfigurationDemo/pub master
└─ ./ChainedConfigurationDemo.exe
Starting demo...
In the end, the greeting is "HelloFromOperatingEnvironment"
Press Ctrl-C to stop the host and exit.
(確保在所有其他情況下在CLI環境中unset Greeting )
對於可選的Azure用例,您將需要
對於#4,此演示使用我的Azure應用程序配置中的只讀連接字符串,該字符串從我的本地系統中插入為環境變量。