. NET Framework configuration Summary

Source: Internet
Author: User
Tags configuration settings
ArticleDirectory
    • 1. Overview
    • 2. Configuration
    • 3. configurationsection and configurationsectiongroup

I tried to improve the configuration file structure of the product a while ago.ProgramSet to see if it can be used as a basis. The following is a summary.

1. Overview

In. NET Framework, Microsoft provides a configuration Assembly that contains the types of programming models used to process configuration data.

1.1 Basic Structure

The basic structure of configuration is as follows:

Configurationmanager is a read/write class of configuration information. For some built-in configuration information, you can directly obtain it from the standard configuration file. In addition, you can also read the specified configuration file to a configuration object, and then read the structured configuration information from the configuration.

The configuration object is the storage container of the configuration file in the memory. You can read the configuration information through the configuration object, or modify the configuration structure or value in the configuration to update the configuration information.

Configurationsection and configurationsectiongroup are structured descriptions of configuration information in the memory. They can form a configuration information tree in the memory.

A set of key-value pairs can be defined in the configurationsection, which is equivalent to a section segment in the INI file and is the basic organizational unit of the configuration item;

Configurationsectiongroup is a collection of subconfigurationsections and subconfigurationsectiongroup. configurationsectiongroup links all configuration data to form a hierarchical tree structure. From the perspective of the configuration information tree, it can be considered that the configurationsection is a leaf node, while the configurationsectiongroup is a non-leaf node. A leaf node contains a set of configuration items.

1.2 configuration file and format

Configuration provides a structured model of configuration data in the memory and its maintenance and storage functions. Users only need to care about the structured data in the memory, and do not need to know how to save to and parse from files. Of course, there is no harm in understanding the configuration file format.

Note: the format of the configuration file read and generated by using the configuration assembly is fixed. The configuration file is transparent during use. You only need to operate on the configuration data in the memory.

Configuration uses XML to save configuration data. The configuration element is the root node. It consists of two parts: Structure Description and data storage.

Configsections, the first subnode of configuration, stores the structure information of configuration data, its subelements can be section and sectiongroup (these subelements correspond to the configurationsection and configurationsectiongroup objects in the sections groups set of configuration objects in memory ), sectiongroup can also contain subsections and subsectiongroup (also corresponding to configurationsection and configurationsectiongroup objects, and its container is the sections and sectiongroups sets of the parent configurationsectiongroup object ).

Each section and sectiongroup have these attributes:

Name-the keyword used to identify the current segment (group. In the data area of the XML file, this value is used as the element name of the data in this section (Group); in memory, this value is also from the parent object's sections groups (or sectiongroups) keyword of this section (group.

Type-Save the type name of the configurationsection (or configurationsectiongroup) object mapped in the memory of this segment (Group). Because the object in sections (or sectiongroups) can be a configurationsection (or configurationsectiongroup) and its derived type. Therefore, you need to save the actual type information of the object in the XML file to restore the original type of object during reading.

In short, the Section and sectiongroup elements correspond to the configurationsection and configurationsectiongroup in the memory. These two elements are used in the file to describe the consistent configuration data structure with the memory.

After the configuration element is the data part, each section and sectiongroup defined in configuration have an element corresponding to it in this part, the element name is the name attribute value of the related section element or sectiongroup element. Its Hierarchical inclusion relationship is the same as that in the structure area, but the element name is different.

The configuration items in each configurationsection correspond to an attribute of the relevant elements in the Data zone. The keywords correspond to the attribute name and values correspond to the attribute value. Note that if the configuration item value in the configurationsection is not changed in the memory, the related elements in the file do not have the corresponding attributes. If all configuration items in the configurationsection remain unchanged as the default value, related data elements are not saved in the file.

2. configuration2.1 ing between configuration and configuration files

Configuration objects are not exactly the same as xml configuration files. Msdn says this class indicates a configuration file that is applicable to a specific computer, application, or resource, or a configuration class instance indicates a merged view of configuration settings. It means that the configuration object can be the Union of configuration information in multiple configuration files.

In fact, if we use openexeconfiguration to open the configuration file of an application, the configuration object will not only contain the configuration information in the specified file, but also contain all machineconfiguration information ), when openexeconfiguration is enabled, the machine configuration of the local machine is automatically opened and merged with the specified configuration file data.

The configuration. Save method writes configuration segments and configuration groups in configration back to their respective files. configuration. saveas combines all configuration segments and configurations and saves them to another file.

2.2 about configurationmanager. openexeconfiguration

When I first used configuration, I was quite speechless by this function. At that time, I did not understand the meaning of the "EXE" in the middle. According to the msdn instructions, "Open the specified client configuration file as a configuration object ", I naturally think that the parameter of this function is the name of the configuration file. So I found a strange phenomenon. after the program is run, the directory is named X. config. config file, and new data is saved to the new file, and X. config does not change. View the filepath attribute of the configration object, which is also X. config. config. At the beginning, I thought this was because the parameter should not contain the file type suffix, So I passed X to this function. The result is that an error message "file not found" is displayed during the runtime.

After searching for various questions and materials, I finally understood the reason. The openexeconfiguration parameter should be the Application Path corresponding to the configuration file to be opened, that is, the passed parameters are the program file path rather than the configuration file path. openexeconfiguration only checks whether the specified program file exists. If it does not exist, it will be abnormal. openexeconfiguration uses <app.exe> + <. config> rules to determine the configuration file name corresponding to the application. If the file does not exist, it is automatically created.

Many people have encountered the same problem. The consistent view is that functions are newly added and may follow the logic of several other functions. In terms of usage, this logic is somewhat weird and unnecessary. All you want is a function to open any configuration file, this configuration file does not need to be associated with a program file, whether it is from the content or file name.

To solve this problem, assume that the configuration file to be opened is X. config, you can create an X file in the same directory. This file does not work, but is only used to check the existence of openexeconfiguration files.

3. configurationsection and configurationsectiongroup3.1 configuration structure description-declarative model

A configurationsection object parsed Based on the configuration file. Its properties include all the attributes of the relevant elements stored in the file, that is, the configuration item. In terms of thinkingCodeIs correct:

However, this Code cannot be compiled because properties are protected attributes. Why? Because we have better usage: as shown below, a class is derived from the exconfigurationsection and an Item1 attribute is defined in it.

With this class, we can directly obtain the configuration data through the item member. Compared with the previous method, this has several advantages: 1. intuitive; 2. The call and type conversion of index properties are encapsulated in customsection1, so you do not have to write the keyword "Item1" everywhere. 3. You can obtain the intelligent sensing support of the editor, which is not prone to errors.

The same is true for configurationsectiongroup. Sub-segments and sub-groups are stored in sections and sectiongroups. You can use keywords to index the required sub-segments or sub-groups, or define an attribute for each sub-segment and sub-group, encapsulate the call and forced conversion of index properties. Example:

This is the declarative model that defines the configuration structure. By hiding keywords in the configuration model, you can avoid the situation where keywords are everywhere. When using keywords, intuitive reference methods and IDE support make code writing easier and more error-prone.

In this way, we do not directly use configurationsection and configurationsectiongroup. Instead, we derive from it and add members representing the configuration data to it. Now we know why the section and sectiongroup elements in the configuration file have the type attribute. This is to correctly restore the corresponding types of objects when reading the configuration file.

This method uses the type information to describe the structure model of the configuration data. In this way, the structure of configuration information will be solidified in the code file and set, not just in the designer's head and based on the content of the configuration file. Obviously, the former is much more reliable than the latter, and there is an implicit benefit: We can use reflection to process the Assembly that contains the defined configuration model and analyze the structure of the configuration data. In some cases, this may be useful.

Note: There is another way to define the configuration structure, known as the programming model, which is unknown for the moment.

3.2 standard configuration information

. NET Framework has some built-in standard configuration information, including machineconfiguration, appsettings, and connectionstrings.

Machineconfiguration is the computer configuration information. It is applicable to all applications on the machine and can be opened using the configurationmanager. openmachineconfiguration method. Machineconfiguration is read from the machine. config file of the currently running. NET Framework version. On my machine, this file contains about 20 segments. The machine. config file is located in the following subdirectory:

% Windir % \ Microsoft. NET \ framework \ version \ config

Appsettings is the configuration data segment of the current application, which can be referenced by configurationmanager. etettings. Read from app.exe. config in the program file directory. This file does not exist at first. After adding content to the deleettings, the file will be created. View the file and you will find that the format is different from that described in section 1.2. That is because the configuration element, that is, the structure part, is ignored, and only the data zone is left.

The configuration object also has the deleettings attribute. If we use openexeconfiguration to open a configuration file, we can obtain the deleettings attribute of the configuration object. Add a configuration item to configuration. deleettings and save the configuration file. Then, you can see the Declaration of deleettings in the configuration file (this will not be ignored at this time ). The memory type of appsettings is system. configuration. appsettingssection.

It can be inferred that configurationmanager. etettings and configuration. appsettings are configuration segments of the same type. However, configurationmanager. appsettings is not a appsettingssection object, but the settings member of appsettingssection.

connectionstrings is the configuration information about the database connection derived from appconfiguration. Its location is the same as that of the deletebench.

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.