前言:
ASP.NET Core 中的應用配置基於配置提供程序 建立的 鍵值對 。
配置提供程序將配置數據從各種配置源讀取到鍵值對。
配置框架的核心實現包是Microsoft Extensions.Configuration
,
依賴於Microsoft Extensions.Configuration.Abstractions
抽象包。
使用了 接口實現分離 的設計模式,使用時引入實現包就可以了。
一、內存配置提供程序
若要激活內存中集合配置,請在 ConfigurationBuilder
的實例上調用 AddInMemoryCollection
擴展方法。
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder();
// 通過 AddInMemoryCollection 的調用使用字典,以提供配置
builder.AddInMemoryCollection(
new Dictionary<string, string>()
{
{ "key1","value1" },
{ "section1:key2","value2" },
{ "section1:section2:key3","value3" }
});
IConfigurationRoot configurationRoot = builder.Build();
Console.WriteLine($"key1:{configurationRoot["key1"]}");
var section = configurationRoot.GetSection("section1");
Console.WriteLine($"key2:{section["key2"]}");
var section2 = section.GetSection("section2");
Console.WriteLine($"key3:{section2["key3"]}");
}
二、命令行配置提供程序
命令行配置需要額外引入Microsoft Extensions.Configuration.CommandLine
包
1) 激活命令行配置
要激活命令行配置,請在 ConfigurationBuilder
的實例上調用 AddCommandLine
擴展方法。
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
// 若要提供命令行參數可替代的應用配置,最後請調用 AddCommandLine
builder.AddCommandLine(args);
}
2) 自變量
命令行配置支持三種格式的命令:
- 無前綴
CommandLineKey1=value1
- 雙劃線 (–)
--CommandLineKey2=value2
,--CommandLineKey2 value2
- 正斜槓 (/)
/CommandLineKey3=value3
,/CommandLineKey3 value3
示例:
dotnet run CommandLineKey1=value1 --CommandLineKey2=value2 /CommandLineKey3=value3
dotnet run --CommandLineKey1 value1 /CommandLineKey2 value2
dotnet run CommandLineKey1= CommandLineKey2=value2
還可以在項目根目錄的 ~/Properties/launchSettings.json
文件中設置調試模式啓動時的命令參數
// ~/Properties/launchSettings.json
{
"profiles": {
"ConfigurationCommandLineDemo": {
"commandName": "Project",
"commandLineArgs": "Key1=val1 --Key2=val2 /Key3=val3"
}
}
}
3) 交換映射
交換映射支持鍵名替換邏輯。 使用 ConfigurationBuilder
手動構建配置時,需要爲 AddCommandLine
方法提供交換替換字典。
交換映射字典鍵規則:
- 交換必須以單劃線 (-) 或雙劃線 (–) 開頭。
- 交換映射字典不得包含重複鍵。
示例:
// dotnet run -k1=val1 -k2=val2 -k3=val3
static void Main(string[] args)
{
var mapper = new Dictionary<string, string> {
{ "-k1", "key1" },
{ "-k2", "key2" },
{ "-k3", "key3" }
};
var builder = new ConfigurationBuilder();
builder.AddCommandLine(args, mapper);
var configurationRoot = builder.Build();
Console.WriteLine($"helper:{configurationRoot["key1"]}");
Console.WriteLine($"helper:{configurationRoot["key2"]}");
Console.WriteLine($"helper:{configurationRoot["key3"]}");
}
三、環境變量配置提供程序
環境變量配置需要額外引入Microsoft Extensions.Configuration.EnvironmentVariables
包
在環境變量中使用分層鍵時,冒號分隔符 (:)
可能無法適用於所有平臺(例如 Bash)。 所有平臺均支持採用雙下劃線 (__)
,並可以用冒號自動替換。
還可以在項目根目錄的 ~/Properties/launchSettings.json
文件中設置調試模式啓動時的環境變量
// ~/Properties/launchSettings.json
{
"profiles": {
"ConfigurationEnvironmentVariablesDemo": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"SECTION__KEY": "val" // 雙下劃線
}
}
}
}
要激活環境變量配置,請在 ConfigurationBuilder
的實例上調用 AddEnvironmentVariables
擴展方法。
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddEnvironmentVariables();
var configurationRoot = builder.Build();
// Development
var env = configurationRoot["ASPNETCORE_ENVIRONMENT"];
// val (雙下劃線作爲分層鍵,也用冒號獲取)
var key = configurationRoot["SECTION:KEY"];
var section = configurationRoot.GetSection("SECTION");
// val
var key1 = section["KEY"];
}
並且環境變量提供程序還支持根據環境變量的前綴來加載
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddEnvironmentVariables("ASPNETCORE_");
var configurationRoot = builder.Build();
// Development
var env = configurationRoot["ENVIRONMENT"];
}
四、文件配置提供程序
FileConfigurationProvider
是從文件系統加載配置的基類。
文件配置提供程序支持指定文件可選和必選,還可以指定是否監視文件變更。
以下配置提供程序專用於特定文件類型:
- INI 配置提供程序 (需要引用
Microsoft Extensions.Configuration.Ini
包) - JSON 配置提供程序 (需要引用
Microsoft Extensions.Configuration.Json
包) - XML 配置提供程序 (需要引用
Microsoft Extensions.Configuration.Xml
包)
1) INI文件
冒號可用作 INI 文件配置中的節分隔符。
[section0]
key0=value
key1=value
[section1]
subsection:key=value
[section2:subsection0]
key=value3
[section2:subsection1]
key=value
若要激活 INI 文件配置,請在 ConfigurationBuilder 的實例上調用 AddIniFile 擴展方法。
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
// AddIniFile 的 optional 參數是設定文件是否可選
// AddIniFile 的 reloadOnChange 參數爲是否重新加載配置
builder.AddIniFile("appsettings.ini");
var configurationRoot = builder.Build();
// value3
var key = configurationRoot["section2:subsection0:key"];
}
2) JSON文件
JsonConfigurationProvider 在運行時期間從 JSON 文件鍵值對加載配置。
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Information"
}
}
}
若要激活 JSON 文件配置,請在 ConfigurationBuilder
的實例上調用 AddJsonFile
擴展方法。
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
// AddJsonFile 的 optional 參數是設定文件是否可選
// AddJsonFile 的 reloadOnChange 參數爲是否重新加載配置
builder.AddJsonFile("appsettings.json");
var configurationRoot = builder.Build();
// Information
var micr = configurationRoot["Logging:LogLevel:Microsoft"];
}