Home  >  Article  >  Backend Development  >  How to implement .Net Core configuration and automatic updates_Practical tips

How to implement .Net Core configuration and automatic updates_Practical tips

韦小宝
韦小宝Original
2017-12-16 09:26:482434browse

The editor below will share with you an article on the implementation method of .Net Core configuration and automatic update. It has a good reference value and I hope it will be helpful to everyone in learning .NET. Friends who are interested in .NET, please follow the editor to take a look.

.Net Core migrated the configuration in the previous Web.Config to the appsettings.json file, and used ConfigurationBuilder to read thisConfiguration file. And you can set it to automatically reload after the configuration file changes, so you don't have 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();


Configuration information reading

Configuration reading It is much more convenient to take than before and you can use it directly. After the ConfigurationBuilder calls the Build() method, you can directly get the value:


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


When the configuration is updated, use Configuration["Section :Key"] also gets the latest value.

Configuring strong typing

You can use strong typing directly and convert the configuration file into your objectUse it directly, As long as the properties of the object correspond one-to-one with the configuration.


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


Then inject ## in the constructor


#

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


##Note: IOptions8742468051c85b06f0a0af9e3e506b5c is a singleton, that is, it will not change when you modify appsettings.json Will change its value, so you must restart your program to update.

Use IOptionsSnapshot8742468051c85b06f0a0af9e3e506b5cAutomatic update

If you want to automatically update your configuration without restarting the program when using strong typing, You can use IOptionsSnapshot8742468051c85b06f0a0af9e3e506b5c

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


The above .Net Core configuration and automatic update implementation method is shared by the editor This is all your content, I hope it can give you a reference, and I hope you will support the PHP Chinese website.

Related recommendations:

How to use EF Core to migrate the database to SQL Server in the .NET Core class library _Practical Tips

Solutions to common problems in deploying asp.net to IIS_Practical Tips

ASP.NET Core Class Library Detailed explanation of how to read configuration files in the project

The above is the detailed content of How to implement .Net Core configuration and automatic updates_Practical tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn