Manage. Net configuration content with multiple files

Source: Internet
Author: User
Tags connectionstrings

In the development of actual. Net ApplicationsProgramWe often use. Net's powerful application configuration function ). As the number of configuration items increases, the configuration file grows and becomes increasingly difficult to manage. In addition, during collaborative development, different configuration items or the same configuration item needs to be set to different values in different environments. For example, we often use connectionstrings to save database connection strings. Each developer may have different database connection strings in the local debugging environment. In this waySource codeAfter synchronizing the configuration file on the server, you must modify the configuration item values to support local debugging. This blog article describes how to use multiple file management. Net configurations to facilitate our daily development.

Basic Principles

According to my personal experience, the configuration file management process generally follows the following principles:

    • In app. config and web. config, only public and infrequently changed configuration items are retained. This ensures the basic stability of this file.
    • Store environment-related configuration items in an independent external configuration file, and then reference this external configuration file in the main configuration file (App. config or web. config. For example, if the database connection string points to a different database in the "Staging" and "product" environments, we can create two independent external configuration files: "connectionstring_product.config" and "connectionstring_stage.config ", in addition, the corresponding database connection string values are included.
    • When using the application setting function in. net, place all configuration items related to this configuration item in an independent external configuration file and reference this external configuration file in the main configuration file. This is because the configuration items related to application setting mainly store the default values of all "setting", which are often long and basically unchanged, saving it to an independent external configuration file makes it easier for us to manage the main configuration file on a daily basis.

The following describes how to reference an external configuration file in the master configuration file.

Implementation

Since the configuration file management mechanisms in. NET 1.1 and. NET 2.0 are inconsistent, I will discuss them separately:

    • . NET 1.1: On the. NET platform of this version, all configuration items are in "<deleetting>. Therefore, a simple method is to use the "file" attribute of "deleetting" to reference an external configuration file. If an external configuration file exists, CLR reads the external configuration file and overwrites the configuration item values in the main configuration file with the configuration item values in the external configuration file. Otherwise, the CLR directly uses the master configuration file.
    • . NET 2.0: In. NET 2.0, configuration items have different "sections" organizations. For example, "<appsetting>" section, "<connectionstrings>" section, you can also customize your own section. However, only "<deleetting>" in all sections still retains the "file" attribute (and also retains the mechanism for merging file attributes and overwriting configuration items ), none of the other "sections" have this attribute. Fortunately, all sections (including grouping etting) have a new property "configsource ". However, the working mechanism of "configsource" is different from that of the original "File. If you set the "configsource" attribute for a section (reference an external configuration file), all configuration items of this section are from the external configuration file, you cannot define new configuration items in the section in the master configuration file at the same time. Because the "configsource" attribute does not have the fusion and overwrite mechanism of the original "file" attribute. For example:

In the main configuration file (App. config or web. config:

<Br/> <connectionstrings configsoure = "user. config"> <br/>

In the external configuration file "user. config:

<Br/> <connectionstrings> <br/> <Add name = "subtextdata" connectionstring = "Server = localhost; database = subtextdata; trusted_connection = true; "/> <br/> </connectionstrings> <br/>

More resources

For. Net configuration file management, you can refer to the following resources:

    • Msdn: http://msdn.microsoft.com/en-us/library/ms229689%28VS.71%29.aspx
    • Managing profiles in a development team: http://codeclimber.net.nz/archive/2007/04/23/Managing-application-configurations-in-development-teams.aspx


Del. icio. US : configuration file, configuration file

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.