. Net Core configuration and automatic update Implementation Method,. netcore

Source: Internet
Author: User

. Net Core configuration and automatic update Implementation Method,. netcore

. Net Core migrates the configuration in Web. Config to the appsettings. json file, and uses ConfigurationBuilder to read the configuration file. You can also set Automatic reload after the configuration file changes, so that you do not need to restart your program.

var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true).AddEnvironmentVariables();

Read configuration information

Configuration reading is much more convenient than before and can be used directly. After the ConfigurationBuilder calls the Build () method, you can directly set the value:

Configuration = builder.Build();var value = Configuration["Section:Key"]

After the Configuration is updated, use Configuration ["Section: Key"] to obtain the latest value.

Configure a strong type

You can directly use a strong type to convert the configuration file to your object for direct use, as long as the object attributes correspond to the configuration.

services.Configure<DatabaseOption>(configuration.GetSection("Database"));

And then inject it into the constructor.

public EntityFrameWorkConfigure(IOptions<DatabaseOption> dataBaseOption){_dataBaseOption = dataBaseOption;}

Note:IOptions <T> is a singleton. That is, when you modify the value of etettings. json, you must restart your program to update it.

Use IOptionsSnapshot <T> to automatically update

If you want to use a strong type, you can automatically update your configuration without restarting the program. You can use IOptionsSnapshot <T>

public EntityFrameWorkConfigure(IOptionsSnapshot<DatabaseOption> dataBaseOption){_dataBaseOption = dataBaseOption;}

The above. Net Core configuration and automatic update Implementation Method is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.