关于配置文件的目录:
在前面我们介绍了,系统中用IConfigurationSource表示不同配置文件的来源,起到读取、设置、加载配置文件的作用。而虚拟类ConfigurationSource继承接口IConfigurationSource,其他类又由ConfigurationSource派生(当然我们也可以写继承自接口IConfigurationSource类,但是没什么必要)。下面是实现不同配置方式的工程:
下面我们主要以测试用例的方式讲解Json与XMl配置文件的源码以及实用方式:
Microsoft.Framework.Configuration.Json
Json的配置文件:在实现的过程中使用Newtonsoft.Json的NuGet程序包,这是非常有名的json操作组件,如果单独涉及到.net的Json操作,推荐使用该组件。
- Json包含数组的使用:
public void ArrayOfObjects() { var json = @"{ 'ip': [ { 'address': '1.2.3.4', 'hidden': false }, { 'address': '5.6.7.8', 'hidden': 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")); }
- 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")); }
- 配置文件的排序后顺序(获取所有子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); }
Microsoft.Framework.Configuration.Xml
xml配置文件,在日常中用的也是比较多的,传统的配置文件就是xml的。[该处实现是支持内容加密的,具体不了解,略]
下面是xml文件的常规用法:
public void SupportAndIgnoreXMLDeclaration() { var xml = @""; 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")); } TestConnectionString SqlClient AnotherTestConnectionString MySql