[Asp.net 5] Configuration-a new generation of Configuration files (multiple implementations of ConfigurationSource ),

Source: Internet
Author: User

[Asp.net 5] Configuration-a new generation of Configuration files (multiple implementations of ConfigurationSource ),

Configuration file directory: [Asp.net 5] Configuration-New Generation Configuration file

As we described earlier, IConfigurationSource is used in the system to indicate the sources of different configuration files, which can be used to read, set, and load configuration files. The virtual class ConfigurationSource inherits the interface IConfigurationSource, and other classes are derived from ConfigurationSource (of course, we can also write the class inherited from the interface IConfigurationSource, but there is no need ). The following are projects that implement different configuration methods:

The following describes the source code and practical methods of the Json and XMl configuration files in the form of test cases:

Microsoft. Framework. Configuration. Json

Json configuration file: Used in the implementation processNewtonsoft. JsonThe NuGet package, which is a very famous json operation component. If the Json operation of. net is involved separately, this component is recommended.

  • Use of arrays containing Json:
Public void ArrayOfObjects () {var json = @ "{'IP': [{'address': '1. 2.3.4 ', 'ddden': false}, {'address': '5. 6.7.8 ', 'den den': true}]} "; var jsonConfigSource = new JsonConfigurationSource (TestStreamHelpers. arbitraryFilePath); jsonConfigSource. load (TestStreamHelpers. stringToStream (json); Assert. equal ("1.2.3.4", jsonConfigSource. get ("ip: 0: address"); Assert. equal ("False", jsonConfigSource. get ("ip: 0: hidden"); Assert. equal ("5.6.7.8", jsonConfigSource. get ("ip: 1: address"); Assert. equal ("True", jsonConfigSource. get ("ip: 1: hidden "));}ArrayOfObjects
  • Use of json multi-configuration files:
Public void ExplicitArrayReplacement () {var json1 = @ "{'IP': ['1. 2.3.4 ', '7. 8.9.10 ', '11. 12.13.14 ']} "; var json2 = @" {'IP': {'1': '15. 16.17.18 '} "; var jsonConfigSource1 = new JsonConfigurationSource (TestStreamHelpers. arbitraryFilePath); jsonConfigSource1.Load (TestStreamHelpers. stringToStream (json1); var jsonConfigSource2 = new JsonConfigurationSource (TestStreamHelpers. arbitraryFilePath); jsonConfigSource2.Load (TestStreamHelpers. stringToStream (json2); var builder = new ConfigurationBuilder (); builder. add (jsonConfigSource1, load: false); builder. add (jsonConfigSource2, load: false); var config = builder. build (); Assert. equal (3, config. getConfigurationSections ("ip "). count (); Assert. equal ("1.2.3.4", config. get ("ip: 0"); Assert. equal ("15.16.17.18", config. get ("ip: 1"); Assert. equal ("11.12.13.14", config. get ("ip: 2 "));}ExplicitArrayReplacement
  • Sorting order of configuration files (key values are sorted when all sub-keys are obtained)

 

Public void PropertiesAreSortedByNumberOnlyFirst () {var json = @ "{'setting': {'hello': 'A', 'bob': 'B', '42 ': 'C', '4': 'D', '10': 'E', '1text': 'F',} "; var jsonConfigSource = new JsonConfigurationSource (TestStreamHelpers. arbitraryFilePath); jsonConfigSource. load (TestStreamHelpers. stringToStream (json); var builder = new ConfigurationBuilder (); builder. add (jsonConfigSource, load: false); var config = builder. build (); var configurationSection = config. getConfigurationSection ("setting"); var indexConfigurationSections = configurationSection. getConfigurationSections (). toArray (); Assert. equal (6, indexConfigurationSections. count (); Assert. equal ("4", indexConfigurationSections [0]. key); Assert. equal ("10", indexConfigurationSections [1]. key); Assert. equal ("42", indexConfigurationSections [2]. key); Assert. equal ("1 text", indexConfigurationSections [3]. key); Assert. equal ("bob", indexConfigurationSections [4]. key); Assert. equal ("hello", indexConfigurationSections [5]. key );}PropertiesAreSortedByNumberOnlyFirst

 

Microsoft. Framework. Configuration. Xml

Xml configuration files are also widely used in daily use. Traditional configuration files are xml. [Content encryption is supported.]

The common usage of xml files is as follows:

Public void SupportAndIgnoreXMLDeclaration () {var xml = @ "<? Xml version = '1. 0' encoding = 'utf-8'?> <Settings> <Data> <DefaultConnection> <ConnectionString> TestConnectionString </ConnectionString> <Provider> SqlClient </Provider> </DefaultConnection> <Inventory> <ConnectionString> AnotherTestConnectionString </ConnectionString> <Provider> MySql </Provider> </Inventory> </Data> </settings> "; var xmlConfigSrc = new XmlConfigurationSource (ArbitraryFilePath); xmlConfigSrc. load (TestStreamHelpers. stringToStream (xml); Assert. equal ("TestConnectionString", xmlConfigSrc. get ("Data: DefaultConnection: ConnectionString"); Assert. equal ("SqlClient", xmlConfigSrc. get ("Data: DefaultConnection: Provider"); Assert. equal ("AnotherTestConnectionString", xmlConfigSrc. get ("Data: Inventory: ConnectionString"); Assert. equal ("MySql", xmlConfigSrc. get ("Data: Inventory: Provider "));}XMLConfigurationSource

 

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.