This commit is contained in:
Hao Kung 2016-06-13 15:05:59 -07:00
Родитель b4c6a630bb
Коммит e1b7960f22
37 изменённых файлов: 387 добавлений и 52 удалений

Просмотреть файл

@ -11,26 +11,44 @@ namespace Microsoft.Extensions.Configuration
/// </summary>
public static class ConfigurationPath
{
/// <summary>
/// The delimiter ":" used to separate individual keys in a path.
/// </summary>
public static readonly string KeyDelimiter = ":";
public static string Combine(params string[] pathSegements)
/// <summary>
/// Combines path segments into one path.
/// </summary>
/// <param name="pathSegments">The path segments to combine.</param>
/// <returns>The combined path.</returns>
public static string Combine(params string[] pathSegments)
{
if (pathSegements == null)
if (pathSegments == null)
{
throw new ArgumentNullException(nameof(pathSegements));
throw new ArgumentNullException(nameof(pathSegments));
}
return string.Join(KeyDelimiter, pathSegements);
return string.Join(KeyDelimiter, pathSegments);
}
public static string Combine(IEnumerable<string> pathSegements)
/// <summary>
/// Combines path segments into one path.
/// </summary>
/// <param name="pathSegments">The path segments to combine.</param>
/// <returns>The combined path.</returns>
public static string Combine(IEnumerable<string> pathSegments)
{
if (pathSegements == null)
if (pathSegments == null)
{
throw new ArgumentNullException(nameof(pathSegements));
throw new ArgumentNullException(nameof(pathSegments));
}
return string.Join(KeyDelimiter, pathSegements);
return string.Join(KeyDelimiter, pathSegments);
}
/// <summary>
/// Extracts the last path segment from the path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>The last path segment of the path.</returns>
public static string GetSectionKey(string path)
{
if (string.IsNullOrEmpty(path))
@ -42,6 +60,11 @@ namespace Microsoft.Extensions.Configuration
return lastDelimiterIndex == -1 ? path : path.Substring(lastDelimiterIndex + 1);
}
/// <summary>
/// Extracts the path corresponding to the parent node for a given path.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>The original path minus the last individual segment found in it. Null if the original path corresponds to a top level node.</returns>
public static string GetParentPath(string path)
{
if (string.IsNullOrEmpty(path))

Просмотреть файл

@ -35,6 +35,10 @@ namespace Microsoft.Extensions.Configuration
/// <returns>The configuration sub-sections.</returns>
IEnumerable<IConfigurationSection> GetChildren();
/// <summary>
/// Returns a <see cref="IChangeToken"/> that can be used to observe when this configuration is reloaded.
/// </summary>
/// <returns>A <see cref="IChangeToken"/>.</returns>
IChangeToken GetReloadToken();
}
}

Просмотреть файл

@ -14,7 +14,7 @@ namespace Microsoft.Extensions.Configuration
/// Builds the <see cref="IConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns></returns>
/// <returns>An <see cref="IConfigurationProvider"/></returns>
IConfigurationProvider Build(IConfigurationBuilder builder);
}
}

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Abstractions of key-value pair based configuration.\r\nCommonly used types:\r\nMicrosoft.Extensions.Configuration.IConfiguration\r\nMicrosoft.Extensions.Configuration.IConfigurationBuilder\r\nMicrosoft.Extensions.Configuration.IConfigurationProvider\r\nMicrosoft.Extensions.Configuration.IConfigurationRoot\r\nMicrosoft.Extensions.Configuration.IConfigurationSection",

Просмотреть файл

@ -10,8 +10,16 @@ using Microsoft.Extensions.Configuration.Binder;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Static helper class that allows binding strongly typed objects to configuration values.
/// </summary>
public static class ConfigurationBinder
{
/// <summary>
/// Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.
/// </summary>
/// <param name="configuration">The configuration instance to bind.</param>
/// <param name="instance">The object to bind.</param>
public static void Bind(this IConfiguration configuration, object instance)
{
if (configuration == null)
@ -25,21 +33,51 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Extracts the value with the specified key and converts it to type T.
/// </summary>
/// <typeparam name="T">The type to convert the value to.</typeparam>
/// <param name="configuration">The configuration.</param>
/// <param name="key">The configuration key for the value to convert.</param>
/// <returns>The converted value.</returns>
public static T GetValue<T>(this IConfiguration configuration, string key)
{
return GetValue(configuration, key, default(T));
}
/// <summary>
/// Extracts the value with the specified key and converts it to type T.
/// </summary>
/// <typeparam name="T">The type to convert the value to.</typeparam>
/// <param name="configuration">The configuration.</param>
/// <param name="key">The configuration key for the value to convert.</param>
/// <param name="defaultValue">The default value to use if no value is found.</param>
/// <returns>The converted value.</returns>
public static T GetValue<T>(this IConfiguration configuration, string key, T defaultValue)
{
return (T)GetValue(configuration, typeof(T), key, defaultValue);
}
/// <summary>
/// Extracts the value with the specified key and converts it to the specified type.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="type">The type to convert the value to.</param>
/// <param name="key">The configuration key for the value to convert.</param>
/// <returns>The converted value.</returns>
public static object GetValue(this IConfiguration configuration, Type type, string key)
{
return GetValue(configuration, type, key, defaultValue: null);
}
/// <summary>
/// Extracts the value with the specified key and converts it to the specified type.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="type">The type to convert the value to.</param>
/// <param name="key">The configuration key for the value to convert.</param>
/// <param name="defaultValue">The default value to use if no value is found.</param>
/// <returns>The converted value.</returns>
public static object GetValue(this IConfiguration configuration, Type type, string key, object defaultValue)
{
var value = configuration.GetSection(key).Value;

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Functionality to bind an object to data in configuration providers for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -6,13 +6,29 @@ using Microsoft.Extensions.Configuration.CommandLine;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Extension methods for registering <see cref="CommandLineConfigurationProvider"/> with <see cref="IConfigurationBuilder"/>.
/// </summary>
public static class CommandLineConfigurationExtensions
{
/// <summary>
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the command line.
/// </summary>
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="args">The command line args.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder configurationBuilder, string[] args)
{
return configurationBuilder.AddCommandLine(args, switchMappings: null);
}
/// <summary>
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from the command line using the specified switch mappings.
/// </summary>
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="args">The command line args.</param>
/// <param name="switchMappings">The switch mappings.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddCommandLine(
this IConfigurationBuilder configurationBuilder,
string[] args,

Просмотреть файл

@ -6,10 +6,18 @@ using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration.CommandLine
{
/// <summary>
/// A command line based <see cref="ConfigurationProvider"/>.
/// </summary>
public class CommandLineConfigurationProvider : ConfigurationProvider
{
private readonly Dictionary<string, string> _switchMappings;
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="args">The command line args.</param>
/// <param name="switchMappings">The switch mappings.</param>
public CommandLineConfigurationProvider(IEnumerable<string> args, IDictionary<string, string> switchMappings = null)
{
if (args == null)
@ -25,8 +33,14 @@ namespace Microsoft.Extensions.Configuration.CommandLine
}
}
/// <summary>
/// The command line arguments.
/// </summary>
protected IEnumerable<string> Args { get; private set; }
/// <summary>
/// Loads the configuration data from the command line args.
/// </summary>
public override void Load()
{
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Просмотреть файл

@ -5,12 +5,26 @@ using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration.CommandLine
{
/// <summary>
/// Represents command line arguments as an <see cref="IConfigurationSource"/>.
/// </summary>
public class CommandLineConfigurationSource : IConfigurationSource
{
/// <summary>
/// Gets or sets the switch mappings.
/// </summary>
public IDictionary<string, string> SwitchMappings { get; set; }
/// <summary>
/// Gets or sets the command line args.
/// </summary>
public IEnumerable<string> Args { get; set; }
/// <summary>
/// Builds the <see cref="CommandLineConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="CommandLineConfigurationProvider"/></returns>
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new CommandLineConfigurationProvider(Args, SwitchMappings);

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Command line configuration provider implementation for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -8,6 +8,9 @@ using System.Linq;
namespace Microsoft.Extensions.Configuration.EnvironmentVariables
{
/// <summary>
/// An environment variable based <see cref="ConfigurationProvider"/>.
/// </summary>
public class EnvironmentVariablesConfigurationProvider : ConfigurationProvider
{
private const string MySqlServerPrefix = "MYSQLCONNSTR_";
@ -20,15 +23,24 @@ namespace Microsoft.Extensions.Configuration.EnvironmentVariables
private readonly string _prefix;
/// <summary>
/// Initializes a new instance.
/// </summary>
public EnvironmentVariablesConfigurationProvider() : this(string.Empty)
{ }
/// <summary>
/// Initializes a new instance with the specified prefix.
/// </summary>
/// <param name="prefix">A prefix used to filter the environment variables.</param>
public EnvironmentVariablesConfigurationProvider(string prefix)
{
_prefix = prefix ?? string.Empty;
}
/// <summary>
/// Loads the environment variables.
/// </summary>
public override void Load()
{
Load(Environment.GetEnvironmentVariables());

Просмотреть файл

@ -1,17 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Extensions.Configuration.EnvironmentVariables
{
/// <summary>
/// Represents environment variables as an <see cref="IConfigurationSource"/>.
/// </summary>
public class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
/// <summary>
/// A prefix used to filter environment variables.
/// </summary>
public string Prefix { get; set; }
/// <summary>
/// Builds the <see cref="EnvironmentVariablesConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="EnvironmentVariablesConfigurationProvider"/></returns>
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new EnvironmentVariablesConfigurationProvider(Prefix);

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Environment variables configuration provider implementation for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -6,6 +6,9 @@ using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Extension methods for <see cref="FileConfigurationProvider"/>.
/// </summary>
public static class FileConfigurationExtensions
{
private static string FileProviderKey = "FileProvider";

Просмотреть файл

@ -13,6 +13,10 @@ namespace Microsoft.Extensions.Configuration
/// </summary>
public abstract class FileConfigurationProvider : ConfigurationProvider
{
/// <summary>
/// Initializes a new instance with the specified source.
/// </summary>
/// <param name="source">The source settings.</param>
public FileConfigurationProvider(FileConfigurationSource source)
{
if (source == null)
@ -29,6 +33,9 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// The source settings for this provider.
/// </summary>
public FileConfigurationSource Source { get; }
private void Load(bool reload)
@ -66,6 +73,10 @@ namespace Microsoft.Extensions.Configuration
Load(reload: false);
}
/// <summary>
/// Loads this provider's data from a stream.
/// </summary>
/// <param name="stream">The stream to read.</param>
public abstract void Load(Stream stream);
}
}

Просмотреть файл

@ -30,6 +30,11 @@ namespace Microsoft.Extensions.Configuration
/// </summary>
public bool ReloadOnChange { get; set; }
/// <summary>
/// Builds the <see cref="IConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="IConfigurationProvider"/></returns>
public abstract IConfigurationProvider Build(IConfigurationBuilder builder);
}
}

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Extension methods for configuring file-based configuration providers for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -8,6 +8,9 @@ using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Extension methods for adding <see cref="IniConfigurationProvider"/>.
/// </summary>
public static class IniConfigurationExtensions
{
/// <summary>

Просмотреть файл

@ -21,8 +21,16 @@ namespace Microsoft.Extensions.Configuration.Ini
/// </examples>
public class IniConfigurationProvider : FileConfigurationProvider
{
/// <summary>
/// Initializes a new instance with the specified source.
/// </summary>
/// <param name="source">The source settings.</param>
public IniConfigurationProvider(IniConfigurationSource source) : base(source) { }
/// <summary>
/// Loads the INI data from a stream.
/// </summary>
/// <param name="stream">The stream to read.</param>
public override void Load(Stream stream)
{
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Просмотреть файл

@ -1,12 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.Extensions.Configuration.Ini
{
/// <summary>
/// An INI file based <see cref="IConfigurationSource"/>.
/// Represents an INI file as an <see cref="IConfigurationSource"/>.
/// Files are simple line structures (<a href="http://en.wikipedia.org/wiki/INI_file">INI Files on Wikipedia</a>)
/// </summary>
/// <examples>
@ -19,6 +17,11 @@ namespace Microsoft.Extensions.Configuration.Ini
/// </examples>
public class IniConfigurationSource : FileConfigurationSource
{
/// <summary>
/// Builds the <see cref="IniConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>An <see cref="IniConfigurationProvider"/></returns>
public override IConfigurationProvider Build(IConfigurationBuilder builder)
{
FileProvider = FileProvider ?? builder.GetFileProvider();

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "INI configuration provider implementation for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -14,8 +14,16 @@ namespace Microsoft.Extensions.Configuration.Json
/// </summary>
public class JsonConfigurationProvider : FileConfigurationProvider
{
/// <summary>
/// Initializes a new instance with the specified source.
/// </summary>
/// <param name="source">The source settings.</param>
public JsonConfigurationProvider(JsonConfigurationSource source) : base(source) { }
/// <summary>
/// Loads the JSON data from a stream.
/// </summary>
/// <param name="stream">The stream to read.</param>
public override void Load(Stream stream)
{
var parser = new JsonConfigurationFileParser();

Просмотреть файл

@ -6,10 +6,15 @@ using System;
namespace Microsoft.Extensions.Configuration.Json
{
/// <summary>
/// A JSON file based <see cref="FileConfigurationSource"/>.
/// Represents a JSON file as an <see cref="IConfigurationSource"/>.
/// </summary>
public class JsonConfigurationSource : FileConfigurationSource
{
/// <summary>
/// Builds the <see cref="JsonConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="JsonConfigurationProvider"/></returns>
public override IConfigurationProvider Build(IConfigurationBuilder builder)
{
FileProvider = FileProvider ?? builder.GetFileProvider();

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "JSON configuration provider implementation for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -10,16 +10,24 @@ using System.Xml;
namespace Microsoft.Extensions.Configuration.Xml
{
/// <summary>
/// An XML file based <see cref="FileConfigurationProvider"/>.
/// Represents an XML file as an <see cref="IConfigurationSource"/>.
/// </summary>
public class XmlConfigurationProvider : FileConfigurationProvider
{
private const string NameAttributeKey = "Name";
/// <summary>
/// Initializes a new instance with the specified source.
/// </summary>
/// <param name="source">The source settings.</param>
public XmlConfigurationProvider(XmlConfigurationSource source) : base(source) { }
internal XmlDocumentDecryptor Decryptor { get; set; } = XmlDocumentDecryptor.Instance;
/// <summary>
/// Loads the XML data from a stream.
/// </summary>
/// <param name="stream">The stream to read.</param>
public override void Load(Stream stream)
{
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Просмотреть файл

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.Extensions.Configuration.Xml
{
/// <summary>
@ -10,6 +8,11 @@ namespace Microsoft.Extensions.Configuration.Xml
/// </summary>
public class XmlConfigurationSource : FileConfigurationSource
{
/// <summary>
/// Builds the <see cref="XmlConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="XmlConfigurationProvider"/></returns>
public override IConfigurationProvider Build(IConfigurationBuilder builder)
{
FileProvider = FileProvider ?? builder.GetFileProvider();

Просмотреть файл

@ -7,6 +7,9 @@ using System.Xml;
namespace Microsoft.Extensions.Configuration.Xml
{
/// <summary>
/// Class responsible for encrypting and decrypting XML.
/// </summary>
public class XmlDocumentDecryptor
{
/// <summary>
@ -18,6 +21,9 @@ namespace Microsoft.Extensions.Configuration.Xml
public static readonly XmlDocumentDecryptor Instance = new XmlDocumentDecryptor();
#endif
/// <summary>
/// Initializes a XmlDocumentDecryptor.
/// </summary>
// don't create an instance of this directly
protected XmlDocumentDecryptor()
{
@ -70,6 +76,11 @@ namespace Microsoft.Extensions.Configuration.Xml
}
}
/// <summary>
/// Override to process encrypted XML.
/// </summary>
/// <param name="document">The document.</param>
/// <returns>An XmlReader which can read the document.</returns>
protected virtual XmlReader DecryptDocumentAndCreateXmlReader(XmlDocument document)
{
// by default we don't know how to process encrypted XML

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "XML configuration provider implementation for Microsoft.Extensions.Configuration.",

Просмотреть файл

@ -6,12 +6,24 @@ using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// IComparer implementation used to order configuration keys.
/// </summary>
public class ConfigurationKeyComparer : IComparer<string>
{
private static readonly string[] _keyDelimiterArray = new[] { ConfigurationPath.KeyDelimiter };
/// <summary>
/// The default instance.
/// </summary>
public static ConfigurationKeyComparer Instance { get; } = new ConfigurationKeyComparer();
/// <summary>
/// Compares two strings.
/// </summary>
/// <param name="x">First string.</param>
/// <param name="y">Second string.</param>
/// <returns></returns>
public int Compare(string x, string y)
{
var xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];

Просмотреть файл

@ -9,31 +9,60 @@ using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Base helper class for implementing an <see cref="IConfigurationProvider"/>
/// </summary>
public abstract class ConfigurationProvider : IConfigurationProvider
{
private ConfigurationReloadToken _reloadToken = new ConfigurationReloadToken();
/// <summary>
/// Initializes a new <see cref="IConfigurationProvider"/>
/// </summary>
protected ConfigurationProvider()
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// The configuration key value pairs for this provider.
/// </summary>
protected IDictionary<string, string> Data { get; set; }
/// <summary>
/// Attempts to find a value with the given key, returns true if one is found, false otherwise.
/// </summary>
/// <param name="key">The key to lookup.</param>
/// <param name="value">The value found at key if one is found.</param>
/// <returns>True if key has a value, false otherwise.</returns>
public virtual bool TryGet(string key, out string value)
{
return Data.TryGetValue(key, out value);
}
/// <summary>
/// Sets a value for a given key.
/// </summary>
/// <param name="key">The configuration key to set.</param>
/// <param name="value">The value to set.</param>
public virtual void Set(string key, string value)
{
Data[key] = value;
}
/// <summary>
/// Loads (or reloads) the data for this provider.
/// </summary>
public virtual void Load()
{
}
/// <summary>
/// Returns the list of keys that this provider has.
/// </summary>
/// <param name="earlierKeys">The earlier keys that other providers contain.</param>
/// <param name="parentPath">The path for the parent IConfiguration.</param>
/// <returns>The list of keys for this provider.</returns>
public virtual IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string parentPath)
@ -53,13 +82,17 @@ namespace Microsoft.Extensions.Configuration
return indexOf < 0 ? key.Substring(prefixLength) : key.Substring(prefixLength, indexOf - prefixLength);
}
/// <summary>
/// Returns a <see cref="IChangeToken"/> that can be used to listen when this provider is reloaded.
/// </summary>
/// <returns></returns>
public IChangeToken GetReloadToken()
{
return _reloadToken;
}
/// <summary>
/// Fires the Change Token
/// Triggers the reload change token and creates a new one.
/// </summary>
protected void OnReload()
{

Просмотреть файл

@ -7,16 +7,35 @@ using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Implements <see cref="IChangeToken"/>
/// </summary>
public class ConfigurationReloadToken : IChangeToken
{
private CancellationTokenSource _cts = new CancellationTokenSource();
/// <summary>
/// Indicates if this token will proactively raise callbacks. Callbacks are still guaranteed to be invoked, eventually.
/// </summary>
public bool ActiveChangeCallbacks => true;
/// <summary>
/// Gets a value that indicates if a change has occured.
/// </summary>
public bool HasChanged => _cts.IsCancellationRequested;
/// <summary>
/// Registers for a callback that will be invoked when the entry has changed. Microsoft.Extensions.Primitives.IChangeToken.HasChanged
/// MUST be set before the callback is invoked.
/// </summary>
/// <param name="callback">The callback to invoke.</param>
/// <param name="state">State to be passed into the callback.</param>
/// <returns></returns>
public IDisposable RegisterChangeCallback(Action<object> callback, object state) => _cts.Token.Register(callback, state);
/// <summary>
/// Used to trigger the change token when a reload occurs.
/// </summary>
public void OnReload() => _cts.Cancel();
}
}

Просмотреть файл

@ -9,11 +9,18 @@ using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// The root node for a configuration.
/// </summary>
public class ConfigurationRoot : IConfigurationRoot
{
private IList<IConfigurationProvider> _providers;
private ConfigurationReloadToken _changeToken = new ConfigurationReloadToken();
/// <summary>
/// Initializes a Configuration root with a list of providers.
/// </summary>
/// <param name="providers">The <see cref="IConfigurationProvider"/>s for this configuration.</param>
public ConfigurationRoot(IList<IConfigurationProvider> providers)
{
if (providers == null)
@ -29,6 +36,11 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Gets or sets the value corresponding to a configuration key.
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
public string this[string key]
{
get
@ -60,6 +72,10 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Gets the immediate children sub-sections.
/// </summary>
/// <returns></returns>
public IEnumerable<IConfigurationSection> GetChildren() => GetChildrenImplementation(null);
internal IEnumerable<IConfigurationSection> GetChildrenImplementation(string path)
@ -71,16 +87,32 @@ namespace Microsoft.Extensions.Configuration
.Select(key => GetSection(path == null ? key : ConfigurationPath.Combine(path, key)));
}
/// <summary>
/// Returns a <see cref="IChangeToken"/> that can be used to observe when this configuration is reloaded.
/// </summary>
/// <returns></returns>
public IChangeToken GetReloadToken()
{
return _changeToken;
}
/// <summary>
/// Gets a configuration sub-section with the specified key.
/// </summary>
/// <param name="key">The key of the configuration section.</param>
/// <returns>The <see cref="IConfigurationSection"/>.</returns>
/// <remarks>
/// This method will never return <c>null</c>. If no matching sub-section is found with the specified key,
/// an empty <see cref="IConfigurationSection"/> will be returned.
/// </remarks>
public IConfigurationSection GetSection(string key)
{
return new ConfigurationSection(this, key);
}
/// <summary>
/// Force the configuration values to be reloaded from the underlying sources.
/// </summary>
public void Reload()
{
foreach (var provider in _providers)

Просмотреть файл

@ -8,12 +8,20 @@ using Microsoft.Extensions.Primitives;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Represents a section of application configuration values.
/// </summary>
public class ConfigurationSection : IConfigurationSection
{
private readonly ConfigurationRoot _root;
private readonly string _path;
private string _key;
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="root">The configuration root.</param>
/// <param name="path">The path to this section.</param>
public ConfigurationSection(ConfigurationRoot root, string path)
{
if (root == null)
@ -30,8 +38,14 @@ namespace Microsoft.Extensions.Configuration
_path = path;
}
/// <summary>
/// Gets the full path to this section from the <see cref="IConfigurationRoot"/>.
/// </summary>
public string Path => _path;
/// <summary>
/// Gets the key this section occupies in its parent.
/// </summary>
public string Key
{
get
@ -45,6 +59,9 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Gets or sets the section value.
/// </summary>
public string Value
{
get
@ -57,6 +74,11 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Gets or sets the value corresponding to a configuration key.
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
public string this[string key]
{
get
@ -70,10 +92,27 @@ namespace Microsoft.Extensions.Configuration
}
}
/// <summary>
/// Gets a configuration sub-section with the specified key.
/// </summary>
/// <param name="key">The key of the configuration section.</param>
/// <returns>The <see cref="IConfigurationSection"/>.</returns>
/// <remarks>
/// This method will never return <c>null</c>. If no matching sub-section is found with the specified key,
/// an empty <see cref="IConfigurationSection"/> will be returned.
/// </remarks>
public IConfigurationSection GetSection(string key) => _root.GetSection(ConfigurationPath.Combine(Path, key));
/// <summary>
/// Gets the immediate descendant configuration sub-sections.
/// </summary>
/// <returns>The configuration sub-sections.</returns>
public IEnumerable<IConfigurationSection> GetChildren() => _root.GetChildrenImplementation(Path);
/// <summary>
/// Returns a <see cref="IChangeToken"/> that can be used to observe when this configuration is reloaded.
/// </summary>
/// <returns></returns>
public IChangeToken GetReloadToken() => _root.GetReloadToken();
}
}

Просмотреть файл

@ -7,6 +7,9 @@ using Microsoft.Extensions.Configuration.Memory;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// IConfigurationBuilder extension methods for the MemoryConfigurationProvider.
/// </summary>
public static class MemoryConfigurationBuilderExtensions
{
/// <summary>

Просмотреть файл

@ -7,10 +7,17 @@ using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration.Memory
{
/// <summary>
/// In-memory implementation of <see cref="IConfigurationProvider"/>
/// </summary>
public class MemoryConfigurationProvider : ConfigurationProvider, IEnumerable<KeyValuePair<string, string>>
{
private readonly MemoryConfigurationSource _source;
/// <summary>
/// Initialize a new instance from the source.
/// </summary>
/// <param name="source">The source settings.</param>
public MemoryConfigurationProvider(MemoryConfigurationSource source)
{
if (source == null)
@ -29,16 +36,29 @@ namespace Microsoft.Extensions.Configuration.Memory
}
}
/// <summary>
/// Add a new key and value pair.
/// </summary>
/// <param name="key">The configuration key.</param>
/// <param name="value">The configuration value.</param>
public void Add(string key, string value)
{
Data.Add(key, value);
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return Data.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();

Просмотреть файл

@ -1,16 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.Extensions.Configuration.Memory
{
/// <summary>
/// Represents in-memory data as an <see cref="IConfigurationSource"/>.
/// </summary>
public class MemoryConfigurationSource : IConfigurationSource
{
/// <summary>
/// The initial key value configuration pairs.
/// </summary>
public IEnumerable<KeyValuePair<string, string>> InitialData { get; set; }
/// <summary>
/// Builds the <see cref="MemoryConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="MemoryConfigurationProvider"/></returns>
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new MemoryConfigurationProvider(this);

Просмотреть файл

@ -3,9 +3,6 @@
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [
"CS1591"
],
"xmlDoc": true
},
"description": "Implementation of key-value pair based configuration for Microsoft.Extensions.Configuration. Includes the memory configuration provider.",