.Net Core 读取配置文件

  • .Net Core 读取配置文件已关闭评论
  • 174 次浏览
  • A+
所属分类:.NET技术

appsetting.json

  1. 用于测试appsetting.json

 {   "Name": "刘德华",   "Teacher": {     "Name": "张无忌",     "Age": "125"   },   "ReadConfigArray": [     "ReadConfigArray_1",     "ReadConfigArray_2",     "ReadConfigArray_3"   ],   "TestObject": {     "Name": "刘德华",     "Teacher": {       "Name": "张无忌",       "Age": "125"     },     "ReadConfigArray": [       "ReadConfigArray_1",       "ReadConfigArray_2",       "ReadConfigArray_3"     ]   } }   

IConfiguration 读取

  1. 直接构造函数注入

Console.WriteLine("name: "+_configuration["Name"]); Console.WriteLine("Teacher:Name " + _configuration["Teacher:Name"]); Console.WriteLine("Teacher:Age " + _configuration["Teacher:Age"]); Console.WriteLine("ReadConfigArray:0 " + _configuration["ReadConfigArray:0"]); Console.WriteLine("ReadConfigArray:1 " + _configuration["ReadConfigArray:1"]); Console.WriteLine("ReadConfigArray:2 " + _configuration["ReadConfigArray:2"]); 

IConfiguration Bind 读取

  1. 使用Bind方法与对象绑定,然后操作对象即可,
  • 注意: 对象属性与配置文件节点相同

using NuGet.DependencyResolver;  namespace Entity {     public class ConfigTestObject     {         public string? Name { get; set; }         public ConfigTestObjectTeacher? Teacher { get; set; }         public List<string>? ReadConfigArray { get;set; }     }      public class ConfigTestObjectTeacher     {         public string? Name { get; set; }         public int Age { get; set; }     } } 
ConfigTestObject configTestObject = new ConfigTestObject(); _configuration.Bind("TestObject", configTestObject);  Console.WriteLine("name: " + configTestObject.Name); Console.WriteLine("Teacher:Name " + configTestObject.Teacher?.Name); Console.WriteLine("Teacher:Age " + configTestObject.Teacher?.Age); Console.WriteLine("ReadConfigArray:0 " + configTestObject.ReadConfigArray?[0]); Console.WriteLine("ReadConfigArray:1 " + configTestObject.ReadConfigArray?[1]); Console.WriteLine("ReadConfigArray:2 " + configTestObject.ReadConfigArray?[2]); 

IOptionsMonitor 读取

  1. Program.cs 读取要读取的节点
  2. 然后在使用的地方构造函数注入IOptionsMonitor 进来使用
  3. configTestObject.CurrentValue 取出对象

Program.cs

builder.Services.Configure<ConfigTestObject>(builder.Configuration.GetSection("TestObject")); 

构造注入

 ConfigTestObject _configTestObject1; public TestIConfigurationController(IOptionsMonitor<ConfigTestObject> configTestObject)  {     _configTestObject1 = configTestObject.CurrentValue; } 

使用

Console.WriteLine("TestIOptionsMonitor..."); Console.WriteLine("name: " + _configTestObject1.Name); Console.WriteLine("Teacher:Name " + _configTestObject1.Teacher?.Name); Console.WriteLine("Teacher:Age " + _configTestObject1.Teacher?.Age); Console.WriteLine("ReadConfigArray:0 " + _configTestObject1.ReadConfigArray?[0]); Console.WriteLine("ReadConfigArray:1 " + _configTestObject1.ReadConfigArray?[1]); Console.WriteLine("ReadConfigArray:2 " + _configTestObject1.ReadConfigArray?[2]); 

IOptions 读取

  1. Program.cs 读取要读取的节点
  2. 然后在使用的地方构造函数注入IOptionsMonitor 进来使用
  3. configTestObject.CurrentValue 取出对象

Program.cs

builder.Services.Configure<ConfigTestObject>(builder.Configuration.GetSection("TestObject")); 

构造注入

 ConfigTestObject _configTestObject1; public TestIConfigurationController(IOptions<ConfigTestObject> configTestObject)  {     _configTestObject1 = configTestObject.Value; } 

使用

Console.WriteLine("TestIOptionsMonitor..."); Console.WriteLine("name: " + _configTestObject1.Name); Console.WriteLine("Teacher:Name " + _configTestObject1.Teacher?.Name); Console.WriteLine("Teacher:Age " + _configTestObject1.Teacher?.Age); Console.WriteLine("ReadConfigArray:0 " + _configTestObject1.ReadConfigArray?[0]); Console.WriteLine("ReadConfigArray:1 " + _configTestObject1.ReadConfigArray?[1]); Console.WriteLine("ReadConfigArray:2 " + _configTestObject1.ReadConfigArray?[2]);