From 7e98d3c1dcb107a7c46cee5019f888268da109b5 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 25 Apr 2016 15:03:31 -0700 Subject: [PATCH] API tweaks - Remove old ReloadOnChange extensions - Add overloads with reloadOnChange --- .../ConfigurationRootExtensions.cs | 94 -------------- .../IniConfigurationExtensions.cs | 29 +++++ .../JsonConfigurationExtensions.cs | 29 +++++ .../XmlConfigurationExtensions.cs | 29 +++++ .../ConfigurationRootExtensionsTest.cs | 117 ------------------ .../ConfigurationTests.cs | 20 +-- 6 files changed, 92 insertions(+), 226 deletions(-) delete mode 100644 src/Microsoft.Extensions.Configuration.FileExtensions/ConfigurationRootExtensions.cs delete mode 100644 test/Microsoft.Extensions.Configuration.FileExtensions.Test/ConfigurationRootExtensionsTest.cs diff --git a/src/Microsoft.Extensions.Configuration.FileExtensions/ConfigurationRootExtensions.cs b/src/Microsoft.Extensions.Configuration.FileExtensions/ConfigurationRootExtensions.cs deleted file mode 100644 index ae7a2ab..0000000 --- a/src/Microsoft.Extensions.Configuration.FileExtensions/ConfigurationRootExtensions.cs +++ /dev/null @@ -1,94 +0,0 @@ -// 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.IO; -using Microsoft.Extensions.FileProviders; - -namespace Microsoft.Extensions.Configuration -{ - public static class FileProviderExtensions - { - public static IConfigurationRoot ReloadOnChanged(this IConfigurationRoot config, string filename) - { - if (config == null) - { - throw new ArgumentNullException(nameof(config)); - } - - if (filename == null) - { - throw new ArgumentNullException(nameof(filename)); - } -#if NET451 - var basePath = AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY") as string ?? - AppDomain.CurrentDomain.BaseDirectory ?? - string.Empty; -#else - var basePath = AppContext.BaseDirectory ?? string.Empty; -#endif - return ReloadOnChanged(config, basePath, filename); - } - - public static IConfigurationRoot ReloadOnChanged( - this IConfigurationRoot config, - string basePath, - string filename) - { - if (config == null) - { - throw new ArgumentNullException(nameof(config)); - } - - if (basePath == null) - { - throw new ArgumentNullException(nameof(basePath)); - } - - if (filename == null) - { - throw new ArgumentNullException(nameof(filename)); - } - - var fileProvider = new PhysicalFileProvider(basePath); - return ReloadOnChanged(config, fileProvider, filename); - } - - public static IConfigurationRoot ReloadOnChanged( - this IConfigurationRoot config, - IFileProvider fileProvider, - string filename) - { - if (config == null) - { - throw new ArgumentNullException(nameof(config)); - } - - if (fileProvider == null) - { - throw new ArgumentNullException(nameof(fileProvider)); - } - - if (filename == null) - { - throw new ArgumentNullException(nameof(filename)); - } - - Action callback = null; - callback = _ => - { - // The order here is important. We need to take the token and then apply our changes BEFORE - // registering. This prevents us from possible having two change updates to process concurrently. - // - // If the file changes after we take the token, then we'll process the update immediately upon - // registering the callback. - var token = fileProvider.Watch(filename); - config.Reload(); - token.RegisterChangeCallback(callback, null); - }; - - fileProvider.Watch(filename).RegisterChangeCallback(callback, null); - return config; - } - } -} diff --git a/src/Microsoft.Extensions.Configuration.Ini/IniConfigurationExtensions.cs b/src/Microsoft.Extensions.Configuration.Ini/IniConfigurationExtensions.cs index d873d7e..875ab21 100644 --- a/src/Microsoft.Extensions.Configuration.Ini/IniConfigurationExtensions.cs +++ b/src/Microsoft.Extensions.Configuration.Ini/IniConfigurationExtensions.cs @@ -57,6 +57,35 @@ namespace Microsoft.Extensions.Configuration }); } + /// + /// Adds the INI configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// Whether the configuration should be reloaded if the file changes. + /// The . + public static IConfigurationBuilder AddIniFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path)); + } + + return AddIniFile(builder, source => + { + source.Path = path; + source.Optional = optional; + source.ReloadOnChange = reloadOnChange; + }); + } + /// /// Adds a INI configuration source to . /// diff --git a/src/Microsoft.Extensions.Configuration.Json/JsonConfigurationExtensions.cs b/src/Microsoft.Extensions.Configuration.Json/JsonConfigurationExtensions.cs index 98d28f1..1ccf068 100644 --- a/src/Microsoft.Extensions.Configuration.Json/JsonConfigurationExtensions.cs +++ b/src/Microsoft.Extensions.Configuration.Json/JsonConfigurationExtensions.cs @@ -60,6 +60,35 @@ namespace Microsoft.Extensions.Configuration }); } + /// + /// Adds the JSON configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// Whether the configuration should be reloaded if the file changes. + /// The . + public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path)); + } + + return AddJsonFile(builder, source => + { + source.Path = path; + source.Optional = optional; + source.ReloadOnChange = reloadOnChange; + }); + } + /// /// Adds a JSON configuration source to . /// diff --git a/src/Microsoft.Extensions.Configuration.Xml/XmlConfigurationExtensions.cs b/src/Microsoft.Extensions.Configuration.Xml/XmlConfigurationExtensions.cs index 71edc7e..ebbdbf4 100644 --- a/src/Microsoft.Extensions.Configuration.Xml/XmlConfigurationExtensions.cs +++ b/src/Microsoft.Extensions.Configuration.Xml/XmlConfigurationExtensions.cs @@ -60,6 +60,35 @@ namespace Microsoft.Extensions.Configuration }); } + /// + /// Adds the XML configuration provider at to . + /// + /// The to add to. + /// Path relative to the base path stored in + /// of . + /// Whether the file is optional. + /// Whether the configuration should be reloaded if the file changes. + /// The . + public static IConfigurationBuilder AddXmlFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentException(Resources.Error_InvalidFilePath, nameof(path)); + } + + return AddXmlFile(builder, source => + { + source.Path = path; + source.Optional = optional; + source.ReloadOnChange = reloadOnChange; + }); + } + /// /// Adds a XML configuration source to . /// diff --git a/test/Microsoft.Extensions.Configuration.FileExtensions.Test/ConfigurationRootExtensionsTest.cs b/test/Microsoft.Extensions.Configuration.FileExtensions.Test/ConfigurationRootExtensionsTest.cs deleted file mode 100644 index 8d42e9f..0000000 --- a/test/Microsoft.Extensions.Configuration.FileExtensions.Test/ConfigurationRootExtensionsTest.cs +++ /dev/null @@ -1,117 +0,0 @@ -// 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.Generic; -using System.Threading; -using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Primitives; -using Xunit; - -namespace Microsoft.Extensions.Configuration -{ - public class ConfigurationRootExtensionsTest - { - [Fact] - public void ReloadOnChanged_GetTokenBeforeReload() - { - // Arrange - var tokenSource1 = new CancellationTokenSource(); - var tokenSource2 = new CancellationTokenSource(); - - var fileProvider = new MockFileProvider(); - fileProvider.Cancel = tokenSource1; - - var configuration = new MockConfigurationRoot(); - configuration.OnReload = () => Assert.Equal(2, fileProvider.WatchCount); - - // Act-1 - configuration.ReloadOnChanged(fileProvider, "config.json"); - - // Assert-1 - Assert.Equal(1, fileProvider.WatchCount); - Assert.Equal(0, configuration.ReloadCount); - - // Act-2 - fileProvider.Cancel = tokenSource2; - tokenSource1.Cancel(); - - Assert.Equal(2, fileProvider.WatchCount); - Assert.Equal(1, configuration.ReloadCount); - } - - private class MockConfigurationRoot : IConfigurationRoot - { - public Action OnReload { get; set; } - - public int ReloadCount { get; private set; } - - public string this[string key] - { - get - { - throw new NotImplementedException(); - } - - set - { - throw new NotImplementedException(); - } - } - - public IEnumerable GetChildren() - { - throw new NotImplementedException(); - } - - public IChangeToken GetReloadToken() - { - throw new NotImplementedException(); - } - - public IConfigurationSection GetSection(string key) - { - throw new NotImplementedException(); - } - - public void Reload() - { - OnReload?.Invoke(); - ReloadCount++; - } - - public void RaiseChanged() - { - throw new NotImplementedException(); - } - - public IDisposable RegisterOnChange(Action callback, object state) - { - throw new NotImplementedException(); - } - } - - private class MockFileProvider : IFileProvider - { - public CancellationTokenSource Cancel { get; set; } - - public int WatchCount { get; private set; } - - public IDirectoryContents GetDirectoryContents(string subpath) - { - throw new NotImplementedException(); - } - - public IFileInfo GetFileInfo(string subpath) - { - throw new NotImplementedException(); - } - - public IChangeToken Watch(string filter) - { - WatchCount++; - return new CancellationChangeToken(Cancel.Token); - } - } - } -} diff --git a/test/Microsoft.Extensions.Configuration.FunctionalTests/ConfigurationTests.cs b/test/Microsoft.Extensions.Configuration.FunctionalTests/ConfigurationTests.cs index b92caf6..c5dbe3a 100644 --- a/test/Microsoft.Extensions.Configuration.FunctionalTests/ConfigurationTests.cs +++ b/test/Microsoft.Extensions.Configuration.FunctionalTests/ConfigurationTests.cs @@ -251,21 +251,11 @@ CommonKey3:CommonKey4=IniValue6"; File.WriteAllText(Path.Combine(_basePath, "reload.ini"), @"IniKey1 = IniValue1"); File.WriteAllText(Path.Combine(_basePath, "reload.xml"), @""); - var config = new ConfigurationBuilder().AddIniFile(source => - { - source.Path = "reload.ini"; - source.ReloadOnChange = true; - }) - .AddJsonFile(source => - { - source.Path = "reload.json"; - source.ReloadOnChange = true; - }) - .AddXmlFile(source => - { - source.Path = "reload.xml"; - source.ReloadOnChange = true; - }).Build(); + var config = new ConfigurationBuilder() + .AddIniFile("reload.ini", optional: false, reloadOnChange: true) + .AddJsonFile("reload.json", optional: false, reloadOnChange: true) + .AddXmlFile("reload.xml", optional: false, reloadOnChange: true) + .Build(); Assert.Equal("JsonValue1", config["JsonKey1"]); Assert.Equal("IniValue1", config["IniKey1"]);