diff --git a/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftConfigurationExtensions.cs b/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftConfigurationExtensions.cs index 5b19985..4e2b85e 100644 --- a/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftConfigurationExtensions.cs +++ b/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftConfigurationExtensions.cs @@ -25,5 +25,13 @@ public static IAppConfigurator ToAppConfigurator(this IConfiguration configurati /// public static IAppConfigurator ToAppConfigurator(this IConfigurationBuilder configuration) => new MicrosoftExtensionsConfigurationBuildRepo(configuration).CreateAppConfigurator(); + + /// + public static IAppConfigurator ToAppConfigurator(this T configuration) + where T : IConfiguration, IConfigurationBuilder + { + IConfigurationBuilder configurationBuilder = configuration; + return configurationBuilder.ToAppConfigurator(); + } } } diff --git a/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftExtensionsConfigurationBuildRepo.cs b/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftExtensionsConfigurationBuildRepo.cs index f851967..741b242 100644 --- a/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftExtensionsConfigurationBuildRepo.cs +++ b/src/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration/MicrosoftExtensionsConfigurationBuildRepo.cs @@ -111,7 +111,9 @@ public void Load() public IEnumerable GetChildKeys(IEnumerable earlierKeys, string parentPath) { - return Array.Empty(); + // 这里如果返回空集合,将会清空原有的配置内容。这个函数的作用是用来进行过滤和追加两个合一起,底层框架这样设计是为了性能考虑。在一个配置管理里面,是由多个 IConfigurationProvider 组成,而多个 IConfigurationProvider 之间,需要有相互影响。在获取所有的 GetChildKeys 时候,假定每个 IConfigurationProvider 都需要追加自身的,那传入 IEnumerable 类型,用于追加是最省资源的。而有些 IConfigurationProvider 之间提供了相同的 Key 的配置,但是有些 IConfigurationProvider 期望覆盖,有些期望不覆盖,于是就通过 earlierKeys 即可用来实现过滤判断,每个不同的 IConfigurationProvider 可以有自己的策略,对先加入的 IConfigurationProvider 返回的 GetChildKeys 进行处理 + // return Array.Empty(); + return earlierKeys; } } } diff --git a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/MicrosoftConfigurationExtensions.cs b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/MicrosoftConfigurationExtensions.cs index 8e84fe8..f6a020d 100644 --- a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/MicrosoftConfigurationExtensions.cs +++ b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/MicrosoftConfigurationExtensions.cs @@ -1,15 +1,43 @@ using System.Collections.Generic; +using System.Linq; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Memory; using Microsoft.Extensions.Primitives; using Microsoft.VisualStudio.TestTools.UnitTesting; using MSTest.Extensions.Contracts; +using ConfigurationManager = Microsoft.Extensions.Configuration.ConfigurationManager; namespace dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests { [TestClass] public class MicrosoftConfigurationExtensions { + [ContractTestCase] + public void WorkWithJson() + { + "通过读取 Json 配置,不会与 AppConfigurator 冲突".Test(() => + { + // Arrange + var configurationManager = new ConfigurationManager(); + configurationManager.AddJsonFile("appsettings.json"); + + // Assert + var logLevelConfigurationList = configurationManager.GetSection("Logging").GetSection("LogLevel").AsEnumerable(true).ToList(); + Assert.AreEqual(2,logLevelConfigurationList.Count); + + // Act + // 接着对接上 AppConfigurator 之后,还能正常获取到配置内容 + var appConfigurator = configurationManager.ToAppConfigurator(); + + Assert.IsNotNull(appConfigurator); + + // Assert + // 期望能获取到和没有对接之前一样的值 + logLevelConfigurationList = configurationManager.GetSection("Logging").GetSection("LogLevel").AsEnumerable(true).ToList(); + Assert.AreEqual(2, logLevelConfigurationList.Count); + }); + } + [ContractTestCase] public void ConfigurationBuilderToAppConfigurator() { @@ -42,9 +70,9 @@ public void ConfigurationToAppConfigurator() const string value = "doubi"; var memoryConfigurationSource = new MemoryConfigurationSource() { - InitialData = new List>() + InitialData = new List>() { - new KeyValuePair(key, value) + new KeyValuePair(key, value) } }; IConfigurationProvider configurationProvider=new MemoryConfigurationProvider(memoryConfigurationSource); diff --git a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/appsettings.json b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/appsettings.json new file mode 100644 index 0000000..ec04bc1 --- /dev/null +++ b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} \ No newline at end of file diff --git a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj index bff3cff..f065bd2 100644 --- a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj +++ b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj @@ -8,7 +8,7 @@ - + @@ -24,4 +24,10 @@ + + + PreserveNewest + + +