我们知道,ConfigurationProvider提供将数据源转换为字典的功能,数据源可以分为很多种,比如:物理文件、数据库、内存变量等等。物理文件又包括很多种类型的文件,比如:xml、json等等。这篇文章旨在讲解各种数据源的ConfigurationProvider对象。
一、MemoryConfigurationProvider
先看下代码:
namespace Microsoft.Extensions.Configuration.Memory{using Microsoft.Extensions.Configuration;using System;using System.Collections;using System.Collections.Generic;public class MemoryConfigurationProvider : ConfigurationProvider, IEnumerable>, IEnumerable{private readonly MemoryConfigurationSource _source;public MemoryConfigurationProvider(MemoryConfigurationSource source){if (source == null){throw new ArgumentNullException("source");}this._source = source;if (this._source.InitialData != null){foreach (KeyValuePair pair in this._source.InitialData){string introduced2 = pair.get_Key();base.Data.Add(introduced2, pair.get_Value());}}}public void Add(string key, string value){base.Data.Add(key, value);}public IEnumerator > GetEnumerator(){return base.Data.GetEnumerator();}IEnumerator IEnumerable.GetEnumerator(){return (IEnumerator) this.GetEnumerator();}}}
可以看到,这个对象继承自ConfigurationProvider和IEnumerable<KeyValuePair<string,string>>,可以知道这个对象本身就是一个字典类型的集合,如果想添加配置可以通过Add方法直接添加。
我们知道,ConfigurationProvider是注册在ConfigurationBuilder对象之上工作的,那么它是怎么注册到这个对象上的呢?看以下代码:
namespace Microsoft.Extensions.Configuration{using Microsoft.Extensions.Configuration.Memory;using System;using System.Collections.Generic;using System.Runtime.CompilerServices;public static class MemoryConfigurationBuilderExtensions{public static IConfigurationBuilder AddInMemoryCollection(this IConfigurationBuilder configurationBuilder){if (configurationBuilder == null){throw new ArgumentNullException("configurationBuilder");}configurationBuilder.Add(new MemoryConfigurationSource());return configurationBuilder;}public static IConfigurationBuilder AddInMemoryCollection(this IConfigurationBuilder configurationBuilder, IEnumerable> initialData){if (configurationBuilder == null){throw new ArgumentNullException("configurationBuilder");}MemoryConfigurationSource source = new MemoryConfigurationSource {InitialData = initialData};configurationBuilder.Add(source);return configurationBuilder;}}}
通过对IConfigurationBuilder的扩展,实现把MemoryConfigurationProvider注册进来。
二、JsonConfigurationProvider
顾名思义,提供对json文件的转换,转换为数据字典。
public class JsonConfigurationProvider : FileConfigurationProvider{// Methodspublic JsonConfigurationProvider(JsonConfigurationSource source);public override void Load(Stream stream);[IteratorStateMachine(typeof(d__3))]private static IEnumerable ReadLines(StreamReader streamReader);private static string RetrieveErrorContext(JsonReaderException e, IEnumerable fileContent);}
是通过以下扩展类实现对ConfigurationBuilder的添加。
public static class JsonConfigurationExtensions{// Methodspublic static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path);public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional);public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange);public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange);}
VS2015创建的带有MVC模板的.net core项目的Startup文件中用法如下:
Optioanal表示如果指定文件不存在不抛出异常而是返回一个空的字典对象。
其他的比如:XmlConfiguationProvider、IniConfigurationProvider类似,不再一一讲解。