1. 程式人生 > >.net core config讀取

.net core config讀取

接口 conf add tar bsp otn stat log create

最簡單的方式

引用

Microsoft.Extensions.Configuration     
Microsoft.Extensions.Configuration.Json      

json文件

新建一個ConfigTest.json
右鍵屬性,設置為“始終復制”
數據如下:

{
  "TotalCount": 2,
  "Students": [
    {
      "Name": "Lili",
      "Sex": "女"
    },
    {
      "Name": "Tom",
      "Sex": "男"
    }
  ]
}

讀取json

var builder = new ConfigurationBuilder();
builder.AddJsonFile("ConfigTest.json");
var config = builder.Build();
Console.WriteLine(config["TotalCount"]);
//索引
Console.WriteLine(config["Students:0:Name"]);
Console.WriteLine(config["Students:0:Sex"]);
Console.WriteLine(config["Students:1:Name"]);
Console.WriteLine(config["Students:1:Sex"]);

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfig方法

Bind讀取配置

引用

第一個示例中的引用也要添加

Microsoft.Extensions.Configuration.Binder

讀取json

var builder = new ConfigurationBuilder()
    .AddJsonFile("ConfigTest.json");
ConfigTest configTest=new ConfigTest();
var config = builder.Build();
//Microsoft.Extensions.Configuration.Binder
config.Bind(configTest);
Console.WriteLine(configTest.TotalCount);
Console.WriteLine(configTest.Students[0].Name);
Console.WriteLine(configTest.Students[0].Sex);
Console.WriteLine(configTest.Students[1].Name);
Console.WriteLine(configTest.Students[1].Sex);

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfigByBind方法

Option

引用

第一個示例中的引用也要添加

Microsoft.Extensions.Options

在appsettings.json中添加測試數據

{
  "MyData": {
    "TotalCount": 2,
    "Students": [
      {
        "Name": "Lili",
        "Sex": "女"
      },
      {
        "Name": "Tom",
        "Sex": "男"
      }
    ]
  } 
}

實體類需要實現IOptions<T>

public class ConfigTest:IOptions<ConfigTest>
{
    public int TotalCount { get; set; }
    public List<Student> Students { get; set; }
    public ConfigTest Value => this;
}

讀取json

public class ConfigController : Controller
{
    private ConfigTest _configTestByOptions;
    private readonly IConfiguration _configuration;
    public ConfigController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public IActionResult Index()
    {
        //GetSection:必須先讀節點
        //ConfigTest實現接口IOptions<ConfigTest>
        _configTestByOptions = _configuration.GetSection("MyData").Get<ConfigTest>();
        return View();
    }
}

註意

CreateDefaultBuilder方法已經讀取appsettings.json,所以在這個示例中,我們沒有調用AddJsonFile方法添加appsettings.json文件,而是直接讀取appsettings.json中的數據即可

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreWebsite/Controllers/ConfigController.cs

熱更新

定義

optional:該配置文件是否可選,如果optional=false,加載失敗會拋出異常
reloadOnChange:文件被修改時是否重新加載,熱更新就是這個,設置為true即可

public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange)

註意,熱更新是修改bin目錄下的生成的文件

示例代碼

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreConsole/Config/ConfigReadDemo.cs 的ReadConfigHotUpdate方法

.net core config讀取