.Net Core 读取配置文件
目录
[*]appsetting.json
[*]IConfiguration 读取
[*]IConfiguration Bind 读取
[*]IOptionsMonitor 读取
[*]Program.cs
[*]构造注入
[*]使用
[*]IOptions 读取
[*]Program.cs
[*]构造注入
[*]使用
appsetting.json
[*]用于测试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 读取
[*]直接构造函数注入
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 读取
[*]使用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?);
Console.WriteLine("ReadConfigArray:1 " + configTestObject.ReadConfigArray?);
Console.WriteLine("ReadConfigArray:2 " + configTestObject.ReadConfigArray?);IOptionsMonitor 读取
[*]Program.cs 读取要读取的节点
[*]然后在使用的地方构造函数注入IOptionsMonitor 进来使用
[*]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?);
Console.WriteLine("ReadConfigArray:1 " + _configTestObject1.ReadConfigArray?);
Console.WriteLine("ReadConfigArray:2 " + _configTestObject1.ReadConfigArray?);IOptions 读取
[*]Program.cs 读取要读取的节点
[*]然后在使用的地方构造函数注入IOptionsMonitor 进来使用
[*]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?);
Console.WriteLine("ReadConfigArray:1 " + _configTestObject1.ReadConfigArray?);
Console.WriteLine("ReadConfigArray:2 " + _configTestObject1.ReadConfigArray?);
来源:https://www.cnblogs.com/qfccc/archive/2023/07/19/17566765.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]