Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new configuration management methods of .NET Core?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the new configuration management methods of .NET Core". In daily operation, I believe that many people have doubts about the new configuration management methods of .NET Core. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the new configuration management methods of .NET Core?" Next, please follow the editor to study!

Read configuration information

Since many people have never come into contact with this newly designed configuration system, in order to give you a sensory understanding of it, let's make a first experience of it from a programming point of view. The API for configuration involves three objects, which are Configuration, ConfigurationBuilder, and ConfigurationProvider, and there are corresponding interfaces in the configuration model to represent them. The relationship between these three objects is very clear. The Configuration object carries the configuration information used in the programming process, and ConfigurationProvider is the provider of the original data source of the configuration information. The communication between the two is completed by ConfigurationBuilder, which uses ConfigurationProvider to extract the source data and convert it into Configuration objects.

2. Detailed explanation of configuration model

In the article "reading configuration information", we introduce several ways to read configuration in the way of example demonstration, in which three important objects are involved, namely, the Configuration that carries the structured configuration information, the ConfigurationProvider that provides the original configuration source data, and the ConfigurationBuilder as the "middleman".

Bind the configuration to various types of objects

For programmatic convenience, instead of directly using the Configuration object created by ConfigurationBuilder to read the value of a single configuration item, we tend to bind a set of related configurations into an object, which we call the Options object. Configuration is logically embodied as a hierarchical configuration tree. For an Options object, if we regard its data members as its child nodes, then the Options object also has a hierarchical attribute structure, so there is no essential difference between the Options object and the configuration at the data structure level. If there is an one-to-one match between the data member definition of the Options type and the structure of the configuration, it is easy to bind the latter to a corresponding type of Options object. ConfigurationBinder, which we focus on in this section, uses this principle to realize the automatic binding of structured configuration to data objects.

Fourth, the story behind the Options model

The core of the whole Options model is two services registered with ServiceCollection. The corresponding service interfaces of these two services are IOptions and IConfigureOptions. The former directly provides the Options object that finally binds the configuration data, while the latter initializes the Options object before it is returned. These two services are registered with the specified ServiceCollection through the extension methods AddOptions and Configure methods, respectively, and the real types of services are OptionsManager and ConfigureFromConfigurationOptions, which are derived from ConfigureOptions.

Diversity of configuration sources: memory variables, environment variables and command line switches

Compared with the traditional configuration system hosted by two XML files, App.config and Web.config, one of the biggest advantages of this new configuration model adopted by ASP.NET Core is the support for a variety of different configuration sources. We can use memory variables, command line arguments, environment variables, and physical files as the source of the original configuration data. If we use physical files as the configuration source, we can choose different formats, such as XML, JSON, INI, and so on. This chapter deals with three configuration sources, namely, memory variables, environment variables, and command line switches.

VI. Diverse configuration sources: JSON files, XML files and INI files

In this article, we will introduce three ConfiguationProvider for physical files, namely, JsonConfiguationProvider for JSON files, XmlConfiguationProvider for XML files, and IniConfiguationProvider for INI files. For these three file types (JSON, XML, and INI), JSON can represent data with different structures in a simple and intuitive format, so it is the best choice for configuration.

7. Various configuration sources: custom ConfiguationProvider supports other configuration sources.

We have given an in-depth and detailed description of the various ConfigurationProvider provided by default in the configuration model, and if they still do not meet the configuration requirements in the project, we can also support our desired configuration source by customizing the ConfigurationProvider. As far as the persistence of configuration data is concerned, it should be a very common way to store the configuration in the database. The next step is to create a ConfigurationProvider for the database, which uses the latest Entity Framework 7 to access the database.

VIII. Synchronization of configuration [example]

When ConfigurationBuilder generates a Configuration object, it uses the ConfigurationProvider registered in it to load the original configuration data, so once the data in the configuration source changes, how can the configuration information used in the application be synchronized with it? If you need to synchronize the configuration information in the application, you need to monitor the original configuration data and reload the configuration data when the data changes. In addition, the reloaded configuration needs to be applied to the program, and we inevitably need a notification mechanism. In order to give readers a sense of the application of configuration synchronization mechanism in specific projects, let's first demonstrate how to achieve real-time synchronization of configuration data through a simple example. We use an INI file as the configuration source, through the implementation of monitoring this file the first time aware of the changes in the contents of the file. Once the contents of the original configuration file change, the application reloads the configuration and applies the new configuration through the registered rollback operation.

IX. Synchronization of configuration [Design]

The so-called "configuration synchronization" in this section is mainly reflected in two aspects: first, how to monitor the configuration source and load its data automatically when it changes, the purpose of which is to synchronize the configuration carried by the Configuration object with the data of the configuration source; second, how to send a notification to the application when the configuration carried by the Configuration object is released, and finally let the application use the latest

At this point, the study on "what are the new configuration management methods for .NET Core" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report