关于配置文件的目录:[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 = @"{
'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"));
}

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

[Asp.net 5] Configuration-新一代的配置文件(ConfigurationSource的多种实现)的更多相关文章

  1. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  2. [Asp.net 5] Configuration-新一代的配置文件

    微软新一代asp.net(vnext),也叫asp.net 5,开源代码都放在网址https://github.com/aspnet下. 本文介绍的是Configuration工程,下载路径为http ...

  3. [Asp.net 5] Configuration-新一代的配置文件(接口定义与基础实现)

    关于配置文件的目录:[Asp.net 5] Configuration-新一代的配置文件 本系列文章讲的是asp.net 5(Asp.net VNext)中的配置文件部分,工程下载地址为:https: ...

  4. [Asp.net 5] Configuration-新一代的配置文件(神奇的Binder)

    关于配置文件的目录:[Asp.net 5] Configuration-新一代的配置文件 之前看过MVC4.0的源码,里面就有Binder.作用是将前台页面传递过来的键值对/字典表绑定到特定的对象.此 ...

  5. asp.net在配置文件里设置多种编码方式的研究

    我们在做asp.net的程序时,在根目录下肯定会有一个web.config的文件, 有点开发经验的可能都知道,它是配置程序的全局信息的地方, 当然了,也可以在这里做更多的事情,下面我们来研究一下 ,如 ...

  6. Asp.net Core 和类库读取配置文件信息

    Asp.net Core 和类库读取配置文件信息 看干货请移步至.net core 读取配置文件公共类 首先开一个脑洞,Asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取 ...

  7. 无法为请求的 Configuration 对象创建配置文件 错误原因

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...

  8. [译]ASP.NET 5 Configuration

    原文:https://docs.asp.net/en/latest/fundamentals/configuration.html ASP.NET 5支持多种配置选项. 应用的配置文件可以是JSON, ...

  9. ASP.NET网站开发中的配置文件

    来源:微信公众号CodeL 1.配置文件层次分类 Machine.config:  对.netframework整体的配置 web.config(framework目录下):  对所有项目所公有的应用 ...

随机推荐

  1. JDBC学习2:为什么要写Class.forName("XXX")?

    Class.forName(String name) 接上一篇JDBC.本来这个内容是放在前面的一篇里面的一起的,后来发现越写越多,想想看就算了,还是单独开一篇文章好了,这样也能写得更加详细点. 上一 ...

  2. OpenSSL密码算法库: MD5示例小程序

    OpenSSL http://www.openssl.org/ OpenSSL整个软件包大概可以分成三个主要的功能部分:密码算法库.SSL协议库以及应用程序.OpenSSL 的密码算法库包含多种加密算 ...

  3. js模版引擎handlebars.js实用教程——由于if功力不足引出的Helper

    返回目录 <!DOCTYPE html> <html> <head> <META http-equiv=Content-Type content=" ...

  4. node.js module初步理解

    在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...

  5. CSS水平垂直居中的几种方法

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  6. Redis中5种数据结构的使用场景介绍

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/108.html?1455861435 一.redis 数据结构使用场景 原 ...

  7. DDD~我们应该知道的Model,DomainModel和ViewModel

      回到目录 图在前 目前项目中可能出现的三种Model模式,对于我们现在开发的一个项目,我觉得使用DDD的思想来设计模型比较清晰,使用DDD的思想把模型model分成了如下三种: 下面是我微博中的截 ...

  8. 说说设计模式~建造者模式(Builder)

    返回目录 建造者模式是我的"设计模式"里创建型模式里的最后一篇,这种模式在实现中,很多架构都用到了,如MVC,MVP,MVVM,它们都是有建造者模式的精髓的,即,创建与表现分享,我 ...

  9. MVVM架构~使用boxy和knockoutjs实现编辑功能

    返回目录 这个功能我认为非常有用,尤其在后台管理系统中,它对用户来说,使用体验这块非常不错,下面是它的截图

  10. Nodejs·进程

    之前对这部分的内容很感兴趣,没想到读起来有点晦涩,还是因为对服务器的知识不是很了解. 说道服务器一般人都会想到tomcat或者Jboss或者weblogic,现在流行起来的Node总让人不太放心,JS ...