標籤:builds title line 資料 on() tin img 添加 nbsp
.NET Core設定檔
在以前.NET中設定檔都是以App.config / Web.config等XML格式的設定檔,而.NET Core中建議使用以JSON為格式的設定檔,因為使用起來更加方面靈活,而且可以使用.NET Core中的DI注入配置資料。
使用:
1 var config = new ConfigurationBuilder()2 .AddInMemoryCollection() //將設定檔的資料載入到記憶體中3 .SetBasePath(Directory.GetCurrentDirectory()) //指定設定檔所在的目錄4 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) //指定載入的設定檔5 .Build(); //編譯成對象 6 Console.WriteLine(config["test"]); //擷取配置中的資料7 config["test"] = "test test"; //修改設定物件的資料,設定物件的資料是可以被修改的8 Console.WriteLine(config["test11"]); //擷取設定檔中不存在資料也是不會報錯的9 Console.WriteLine(config["theKey:nextKey"]); //擷取:theKey -> nextKey 的值
設定檔appsettings.json檔案內容:
1 {2 "test": "testVal",3 "theKey": {4 "nextKey": "keyVal"5 }6 }
注意:
ConfigurationBuilder需要添加包:"Microsoft.Extensions.Configuration"
AddJsonFile需要添加包:"Microsoft.Extensions.Configuration.Json"
與DI配合使用
1 var sp = new ServiceCollection() 2 .AddOptions() //注入IOptions<T>,這樣就可以在DI容器中擷取IOptions<T>了 3 .Configure<TestCls>(config.GetSection("TestCls")) //注入配置資料 4 //也可以對注入的配置資料進行修改 5 .Configure<TestCls>(t => 6 { 7 t.Name = "Jame"; //修改Name的值 8 }) 9 .BuildServiceProvider(); //編譯10 11 var test = sp.GetService<IOptions<TestCls>>(); //擷取注入的配置資料對象12 Console.WriteLine(JsonConvert.SerializeObject(test.Value)); //{"Name":"Jame","Age":123}13 14 //下面的代碼中檢驗Configure注入的配置資料對象是單例模式的(.NET Core中DI容器的三種生命週期:Singleton(單例), Scoped(範圍), Transient(瞬態))15 var test1 = sp.GetService<IOptions<TestCls>>();16 Console.WriteLine(test == test1); //true17 //建立一個新的範圍擷取配置資料對象18 var test2 = sp.GetService<IServiceScopeFactory>().CreateScope().ServiceProvider.GetService<IOptions<TestCls>>();19 Console.WriteLine(test == test2); //true
配置測試類別:
1 public class TestCls2 {3 public string Name { get; set; }4 public int Age { get; set; }5 }
appsettings.json中的內容:
1 {2 "TestCls": {3 "Name": "Tom",4 "Age": 1235 }6 }
注意:
ServiceCollection需要添加包: "Microsoft.Extensions.DependencyInjection"
AddOptions需要添加包: "Microsoft.Extensions.Options.ConfigurationExtensions"
ASP.NET Core中使用
Startup.cs -> Startup構造方法中進行初始化設定檔:
1 var builder = new ConfigurationBuilder()2 .AddInMemoryCollection()3 .SetBasePath(env.ContentRootPath)4 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)5 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);6 Configuration = builder.Build();
Startup.cs -> ConfigureServices方法中進行注入配置資料:
1 services.AddOptions() //注入IOptions<T>2 .Configure<TestCls>(Configuration.GetSection(nameof(TestCls)))3 .Configure<TestCls>(test =>4 {5 test.Name = "Jame"; //修改Name的值6 });
設定檔中的配置資料:
1 { 2 "Logging": { 3 "IncludeScopes": false, 4 "LogLevel": { 5 "Default": "Debug", 6 "System": "Information", 7 "Microsoft": "Information" 8 } 9 },10 "TestCls": {11 "Name": "Tom",12 "Age": 12313 }14 }
注入到控制器中:
1 [Route("api/[controller]")] 2 public class ValuesController : Controller 3 { 4 IOptions<TestCls> _test; 5 public ValuesController(IOptions<TestCls> test) 6 { 7 _test = test; 8 } 9 [HttpGet]10 public string Gets()11 {12 return JsonConvert.SerializeObject(_test.Value);13 }
.NET Core設定檔載入與DI注入配置資料