国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Asp.net 5] Configuration-新一代的配置文件(ConfigurationSource的多種實現)

2019-11-17 02:21:20
字體:
來源:轉載
供稿:網友

[asp.net 5] Configuration-新一代的配置文件(ConfigurationSource的多種實現)

關于配置文件的目錄:[Asp.net 5] Configuration-新一代的配置文件

在前面我們介紹了,系統中用IConfigurationSource表示不同配置文件的來源,起到讀取、設置、加載配置文件的作用。而虛擬類ConfigurationSource繼承接口IConfigurationSource,其他類又由ConfigurationSource派生(當然我們也可以寫繼承自接口IConfigurationSource類,但是沒什么必要)。下面是實現不同配置方式的工程:

下面我們主要以測試用例的方式講解Json與xml配置文件的源碼以及實用方式:

Microsoft.Framework.Configuration.Json

Json的配置文件:在實現的過程中使用Newtonsoft.Json的NuGet程序包,這是非常有名的json操作組件,如果單獨涉及到.net的Json操作,推薦使用該組件。

  • Json包含數組的使用:
public void ArrayOfObjects()        {            var json = @"{                'ArrayOfObjects
  • json多配置文件的使用:
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
  • 配置文件的排序后順序(獲取所有子key的時候會對key值進行排序)

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("1text", indexConfigurationSections[3].Key);            Assert.Equal("bob", indexConfigurationSections[4].Key);            Assert.Equal("hello", indexConfigurationSections[5].Key);        }
PropertiesAreSortedByNumberOnlyFirst

Microsoft.Framework.Configuration.Xml

xml配置文件,在日常中用的也是比較多的,傳統的配置文件就是xml的。[該處實現是支持內容加密的,具體不了解,略]

下面是xml文件的常規用法:

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岚皋县| 乃东县| 平舆县| 三明市| 杭锦旗| 凤翔县| 晋州市| 额尔古纳市| 宜良县| 寿光市| 化隆| 齐河县| 尖扎县| 工布江达县| 固镇县| 北碚区| 宣恩县| 玉林市| 荃湾区| 武威市| 临湘市| 定陶县| 彰化市| 鹰潭市| 沭阳县| 武功县| 米林县| 乐安县| 海伦市| 望城县| 九台市| 绵竹市| 会宁县| 北京市| 九寨沟县| 金平| 广宗县| 克拉玛依市| 宜丰县| 连平县| 梁平县|