ConfigurationRoot
Represents the root node for a configuration.
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Microsoft.Extensions.Configuration
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[DebuggerDisplay("{DebuggerToString(),nq}")]
[DebuggerTypeProxy(typeof(ConfigurationRootDebugView))]
public class ConfigurationRoot : IConfigurationRoot, IConfiguration, IDisposable
{
private sealed class ConfigurationRootDebugView
{
private readonly ConfigurationRoot _current;
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public ConfigurationSectionDebugView[] Items {
get {
return ConfigurationSectionDebugView.FromConfiguration(_current, _current).ToArray();
}
}
public ConfigurationRootDebugView(ConfigurationRoot current)
{
_current = current;
}
}
private readonly IList<IConfigurationProvider> _providers;
private readonly List<IDisposable> _changeTokenRegistrations;
private ConfigurationReloadToken _changeToken = new ConfigurationReloadToken();
public IEnumerable<IConfigurationProvider> Providers => _providers;
[System.Runtime.CompilerServices.Nullable(2)]
public string this[string key] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
return GetConfiguration(_providers, key);
}
[param: System.Runtime.CompilerServices.Nullable(2)]
set {
SetConfiguration(_providers, key, value);
}
}
public ConfigurationRoot(IList<IConfigurationProvider> providers)
{
System.ThrowHelper.ThrowIfNull(providers, "providers");
_providers = providers;
_changeTokenRegistrations = new List<IDisposable>(providers.Count);
foreach (IConfigurationProvider provider in providers) {
provider.Load();
List<IDisposable> changeTokenRegistrations = _changeTokenRegistrations;
IConfigurationProvider configurationProvider = provider;
changeTokenRegistrations.Add(ChangeToken.OnChange(configurationProvider.GetReloadToken, RaiseChanged));
}
}
public IEnumerable<IConfigurationSection> GetChildren()
{
return this.GetChildrenImplementation(null);
}
public IChangeToken GetReloadToken()
{
return _changeToken;
}
public IConfigurationSection GetSection(string key)
{
return new ConfigurationSection(this, key);
}
public void Reload()
{
foreach (IConfigurationProvider provider in _providers) {
provider.Load();
}
RaiseChanged();
}
private void RaiseChanged()
{
Interlocked.Exchange(ref _changeToken, new ConfigurationReloadToken()).OnReload();
}
public void Dispose()
{
foreach (IDisposable changeTokenRegistration in _changeTokenRegistrations) {
changeTokenRegistration.Dispose();
}
foreach (IConfigurationProvider provider in _providers) {
(provider as IDisposable)?.Dispose();
}
}
[return: System.Runtime.CompilerServices.Nullable(2)]
internal static string GetConfiguration(IList<IConfigurationProvider> providers, string key)
{
for (int num = providers.Count - 1; num >= 0; num--) {
if (providers[num].TryGet(key, out string value))
return value;
}
return null;
}
internal static void SetConfiguration(IList<IConfigurationProvider> providers, string key, [System.Runtime.CompilerServices.Nullable(2)] string value)
{
if (providers.Count == 0)
throw new InvalidOperationException(System.SR.Error_NoSources);
foreach (IConfigurationProvider provider in providers) {
provider.Set(key, value);
}
}
private string DebuggerToString()
{
return $"""{ConfigurationSectionDebugView.FromConfiguration(this, this).Count}";
}
}
}