SoFunction
Updated on 2025-04-08

.Net Core configuration file read IOptions, IOptionsMonitor, IOptionsSnapshot

Preface

As we all know, configuration files are the major innovation of .Net. It put aside the complicated xml files in the past and uses a more concise and easy-to-understand json method. It's simply too comfortable! Although things are good, how to read this configuration in the program is a pit every novice must overcome (of course, it also includes me as a dog).

Injection is unknown when encountering things. As long as you encounter something you don’t know what to do, you must first think about injecting it conveniently. With this configuration file, the framework must be equipped with a simple and direct reading API. According to my habit, directly upload the code:

First, we add the Demo configuration node to the configuration file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "": "Warning"
    }
  },
  "Demo": {
    "Value1": "1",
    "Value2": "2",
    "Value3": "3",
    "Value4": "4"
  }
}

Inject the IConfiguration service interface where configuration files are required

private readonly IConfiguration _configuration;

public ValuesController(IConfiguration configuration)
{
    _configuration = configuration;
}

Generally, our more direct way is to obtain the corresponding configuration node through GetSection, and then obtain the corresponding configuration items.

var section = _configuration.GetSection("Demo");
var value1 = ("Value1", "1");

If there are deeper nodes in the Demo node, GetSection can go deeper to the corresponding next node through:

 

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "": "Warning"
    }
  },
  "Demo": {
    "Value1": "1",
    "Value2": "2",
    "Value3": "3",
    "Value4": "4",
    "Model" {
      "Name": "Waiter",
      "Phone": "12345678911"
    }
  }
}
var model = _configuration.GetSection("Demo:Model");

Some friends may ask, and every place I need to use is directly used to read the configuration using strings as parameters. If the configuration item is suddenly changed in the future, wouldn't it be very troublesome;

Friends, please rest assured that you can define an entity class and bind it. If there are any changes in the future, just rename the corresponding attributes directly. The example code

Method 1:

var options = new DemoOptions();
_configuration.GetSection("Demo").Bind(options);

Method 2:

var options2 = _configuration.GetSection("Demo").Get<DemoOptions>();

Method 3:Use dependency injection method in , .Net 6 or above, the following is .Net 6

<DemoOptions>(("Demo"));

Inject IOptions<TOptions> where you need to use it to get the configuration value. It should be noted that IOptions is a Singleton service, that is, register when the application is started, and then change the configuration file. The IOptions will not be updated synchronously and will still be the old value.

private readonly DemoOptions _demoOptions;
public ValuesController(IOptions<DemoOptions> options)
{
    _demoOptions = ;
}

If the configuration needs to be hot updated, you only need to inject IOptionsMonitor<TOptions> or IOptionsSnapshot<TOptions>; the life cycle of IOptionsSnapshot<TOptions> is Scoped, and the configuration will be re-acquisitioned every request; the life cycle of IOptionsSnapshot<TOptions> is a singleton, and the difference from IOptions<TOptions> is that when the configuration file changes, the response will be automatically synchronized.

This is the article about reading IOptions, IOptionsMonitor, and IOptionsSnapshot in .Net Core configuration file. For more related contents of reading .Net Core files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!