. NET Core configuration file loading and DI injection configuration data, coredi
. NET Core configuration file
Before. the configuration files in. NET are all App. config/Web. config and other configuration files in XML format. NET Core, we recommend that you use a configuration file in JSON format, because it is more flexible to use and can be used.. NET Core.
Usage:
1 var config = new ConfigurationBuilder () 2. addInMemoryCollection () // load the configuration file data to the memory. setBasePath (Directory. getCurrentDirectory () // specify the directory where the configuration file is located 4. addJsonFile ("deleteworkflow. json ", optional: true, reloadOnChange: true) // specify the loaded configuration file 5. build (); // compile to object 6 Console. writeLine (config ["test"]); // get the data in the configuration. 7. config ["test"] = "test"; // modify the data of the configuration object, the data of the configuration object is the 8 Console that can be modified. writeLine (config ["test11"]); // obtain the Console that does not contain any data in the configuration file. writeLine (config ["theKey: nextKey"]); // get the value of theKey-> nextKey
Configuration file:
1 {2 "test": "testVal",3 "theKey": {4 "nextKey": "keyVal"5 }6 }
Note:
Add a package for ConfigurationBuilder: "Microsoft. Extensions. Configuration"
AddJsonFile: "Microsoft. Extensions. Configuration. Json"
Use with DI
1 var sp = new ServiceCollection () 2. addOptions () // inject IOptions <T>, so that you can obtain IOptions in the di container <T> 3. configure <TestCls> (config. getSection ("TestCls") // inject configuration data 4 // You can also modify the injected configuration data 5. configure <TestCls> (t => 6 {7 t. name = "Jame"; // modify the Name value 8}) 9. buildServiceProvider (); // compile 10 11 var test = sp. getService <IOptions <TestCls> (); // obtain the injected configuration data object 12 Console. writeLine (JsonConvert. serializeObject (test. value); // {"Name": "Jame", "Age ": 123} 13 14 // the following code verifies that the configuration data object injected by Configure is in singleton mode (. three life cycles of DI containers in NET Core: Singleton (Singleton), Scoped (scope), Transient (Transient) 15 var test1 = sp. getService <IOptions <TestCls> (); 16 Console. writeLine (test = test1); // true17 // create a new scope to obtain the configuration data object 18 var test2 = sp. getService <IServiceScopeFactory> (). createid (). serviceProvider. getService <IOptions <TestCls> (); 19 Console. writeLine (test = test2); // true
Configuration test class:
1 public class TestCls2 {3 public string Name { get; set; }4 public int Age { get; set; }5 }
The content in etettings. json is as follows:
1 {2 "TestCls": {3 "Name": "Tom",4 "Age": 1235 }6 }
Note:
ServiceCollection requires a package: "Microsoft. Extensions. DependencyInjection"
Add package for AddOptions: "Microsoft. Extensions. Options. ConfigurationExtensions"
Used in ASP. NET Core
Initialize the configuration file in Startup. cs-> Startup constructor:
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 method to inject configuration data:
1 services. addOptions () // inject IOptions <T> 2. configure <TestCls> (Configuration. getSection (nameof (TestCls) 3. configure <TestCls> (test => 4 {5 test. name = "Jame"; // modify the Name value 6 });
Configuration data in the configuration file:
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 }
Inject to the Controller:
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 }
Access:/api/values
Display: {"Name": "Jame", "Age": 123}