Technology Sharing

Note: How to use Microsoft.Extensions.Options

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

First, the purpose:

Microsoft.Extensions.Options is a library for handling configuration options in .NET Core. It provides a strongly typed way to read and bind configuration data (such as data from appsettings.json files, environment variables, or other configuration sources) and inject it into the application. This library is part of .NET Core, but can also be used in .NET Framework or other .NET implementations.
Core idea


• Options pattern: The Options pattern uses classes to represent groupings of configuration data. These classes are usually simple POCOs (Plain Old CLR Objects) that contain some properties that correspond to the keys of the configuration data.
• IOptions: IOptions<T> The interface is used to access configuration data of type T. Once you register configuration data of type T, you can access this data in other parts of your application through dependency injection.
• IOptionsSnapshot: IOptionsSnapshot<T> It is suitable for scenarios where you need to reload configuration data while the application is running. It provides a new snapshot of the configuration data on each request.
• IOptionsMonitor: IOptionsMonitor<T> Used to monitor changes in configuration data in real time. It provides a change notification event that can be triggered when configuration data changes.


2. Example:


Assume you have an appsettings.json configuration file that contains some application settings:

  1. {
  2. "MySettings": {
  3. "SettingA": "value1",
  4. "SettingB": "value2"
  5. }
  6. }

First, define a class to represent these settings:

  1. public class MySettings
  2. {
  3. public string SettingA { get; set; }
  4. public string SettingB { get; set; }
  5. }

Then, register this configuration class in the ConfigureServices method of Startup.cs:

 

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. // 绑定配置
  4. services.Configure<MySettings>(Configuration.GetSection("MySettings"));
  5. // 其他服务注册...
  6. }

Now you can access these settings via dependency injection wherever you need them:

  1. public class MyService
  2. {
  3. private readonly MySettings _mySettings;
  4. public MyService(IOptions<MySettings> options)
  5. {
  6. _mySettings = options.Value;
  7. }
  8. public void DoSomething()
  9. {
  10. Console.WriteLine(_mySettings.SettingA);
  11. // 使用 _mySettings...
  12. }
  13. }

Microsoft.Extensions.Options provides a simple and powerful way to manage and access your application's configuration data. By using strongly typed configuration classes and dependency injection, you can easily integrate configuration data into your application while keeping your code clean and maintainable.

5. Things to know

IOptions 

IOptionsSnapshot 

IOptionsMonitor 

Microsoft.Extensions.Options Namespace | Microsoft Learn 

System.Windows.Controls Namespace | Microsoft Learn

6. Source code address

GitHub - HeBianGu/WPF-ControlDemo: Example

GitHub - HeBianGu/WPF-ControlBase: WPF-encapsulated custom control resource library

GitHub - HeBianGu/WPF-Control: WPF lightweight control and skin library

7. Learn more

System.Windows.Controls Namespace | Microsoft Learn

https://github.com/HeBianGu

HeBianGu's personal space - HeBianGu's personal homepage - Bilibili video