This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3044|Reply: 0

[.NET Core] ASP.NET Core (twenty-one) configuration options are the difference between AddOptions and Configure

[Copy link]
Posted on 6/18/2023 7:56:45 PM | | | |
Requirements: While reading the ASP.NET Core options documentation, I found that there are two types of code that use classes for option patterns to provide strongly typed access to the relevant settings group"AddOptions”、“ConfigureWhat is the difference between the two?

The preferred method for reading the relevant configuration values is to use the option mode.

Option mode in ASP.NET Core:The hyperlink login is visible.

The ConsulOption object looks like this:

appsettings.json The configuration is as follows:

AddOptions configuration

The code is as follows:


Configure configuration

The code is as follows:


The two are actually equivalent, the underlying call of AddOptions Bind is still the Configure method.

OptionsBuilder.Bind(IConfiguration config) actually calls Configure(IConfiguration config) directly, so they are also equivalent, so the two APIs are interchangeable, and AddOptions later came along and allowed for more customizations.

OptionsBuilderConfigurationExtensionsCode:The hyperlink login is visible.



Read the configuration

IOptions<TOptions>: Singleton service, that is, the application is registered when it is launched, and the configuration file is subsequently changed, the IOptions will not be updated synchronously, and it will still be the old value
IOptionsSnapshot<TOptions>: The lifecycle is scoped, and the configuration is re-acquired with each request.
IOptionsMonitor<TOptions>: The lifecycle is a singleton, unlike IOptions, which<TOptions> listens for updates occurring in the profile and automatically synchronizes the response.

(End)





Previous:Consul installation tutorial in a Windows test environment
Next:EF SQL statements automatically generate IS NULL or IS NOT NULL
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com