From 7ba01a6555a047c12e0775755392cb6a33fe1eb9 Mon Sep 17 00:00:00 2001 From: lindexi Date: Fri, 23 Jun 2023 10:32:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=98=AF=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestWebApplication/Program.cs | 20 ++++++++++++++ .../Properties/launchSettings.json | 27 +++++++++++++++++++ TestWebApplication/TestWebApplication.csproj | 13 +++++++++ .../appsettings.Development.json | 8 ++++++ TestWebApplication/appsettings.json | 9 +++++++ dotnetCampus.Configurations.sln | 12 +++++++-- 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 TestWebApplication/Program.cs create mode 100644 TestWebApplication/Properties/launchSettings.json create mode 100644 TestWebApplication/TestWebApplication.csproj create mode 100644 TestWebApplication/appsettings.Development.json create mode 100644 TestWebApplication/appsettings.json diff --git a/TestWebApplication/Program.cs b/TestWebApplication/Program.cs new file mode 100644 index 0000000..3107127 --- /dev/null +++ b/TestWebApplication/Program.cs @@ -0,0 +1,20 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +var configuration = app.Configuration.GetSection("Logging").GetSection("LogLevel"); +foreach (var keyValuePair in configuration.AsEnumerable(true)) +{ + +} + +var appConfigurator = ((IConfigurationBuilder) app.Configuration).ToAppConfigurator(); + +configuration = app.Configuration.GetSection("Logging").GetSection("LogLevel"); +foreach (var keyValuePair in configuration.AsEnumerable(true)) +{ + +} + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/TestWebApplication/Properties/launchSettings.json b/TestWebApplication/Properties/launchSettings.json new file mode 100644 index 0000000..ff3ec79 --- /dev/null +++ b/TestWebApplication/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56715", + "sslPort": 0 + } + }, + "profiles": { + "TestWebApplication": { + "commandName": "Project", + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5264", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/TestWebApplication/TestWebApplication.csproj b/TestWebApplication/TestWebApplication.csproj new file mode 100644 index 0000000..326981f --- /dev/null +++ b/TestWebApplication/TestWebApplication.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/TestWebApplication/appsettings.Development.json b/TestWebApplication/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/TestWebApplication/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/TestWebApplication/appsettings.json b/TestWebApplication/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/TestWebApplication/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/dotnetCampus.Configurations.sln b/dotnetCampus.Configurations.sln index b9842b8..87df83b 100644 --- a/dotnetCampus.Configurations.sln +++ b/dotnetCampus.Configurations.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29503.13 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{40BD8E4E-62CC-4055-8547-96D733254C87}" ProjectSection(SolutionItems) = preProject @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Configurations EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests", "tests\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj", "{4AB10B28-5DC6-42FE-8966-5CBC4EB19845}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWebApplication", "TestWebApplication\TestWebApplication.csproj", "{DE36E375-3A89-48C7-A6F8-2A332754BC25}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -68,6 +70,12 @@ Global {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.Release|Any CPU.Build.0 = Release|Any CPU {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.UnitTest|Any CPU.ActiveCfg = Release|Any CPU {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.UnitTest|Any CPU.Build.0 = Release|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Release|Any CPU.Build.0 = Release|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.UnitTest|Any CPU.ActiveCfg = Debug|Any CPU + {DE36E375-3A89-48C7-A6F8-2A332754BC25}.UnitTest|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 45eef30af97ff08d2be88aa146d3ada47fab6173 Mon Sep 17 00:00:00 2001 From: lindexi Date: Fri, 23 Jun 2023 10:32:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Revert=20"=E5=8A=A0=E4=B8=8A=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=B5=8B=E8=AF=95=20=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E6=98=AF=E7=A9=BA"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 7ba01a6555a047c12e0775755392cb6a33fe1eb9. --- TestWebApplication/Program.cs | 20 -------------- .../Properties/launchSettings.json | 27 ------------------- TestWebApplication/TestWebApplication.csproj | 13 --------- .../appsettings.Development.json | 8 ------ TestWebApplication/appsettings.json | 9 ------- dotnetCampus.Configurations.sln | 12 ++------- 6 files changed, 2 insertions(+), 87 deletions(-) delete mode 100644 TestWebApplication/Program.cs delete mode 100644 TestWebApplication/Properties/launchSettings.json delete mode 100644 TestWebApplication/TestWebApplication.csproj delete mode 100644 TestWebApplication/appsettings.Development.json delete mode 100644 TestWebApplication/appsettings.json diff --git a/TestWebApplication/Program.cs b/TestWebApplication/Program.cs deleted file mode 100644 index 3107127..0000000 --- a/TestWebApplication/Program.cs +++ /dev/null @@ -1,20 +0,0 @@ -var builder = WebApplication.CreateBuilder(args); -var app = builder.Build(); - -var configuration = app.Configuration.GetSection("Logging").GetSection("LogLevel"); -foreach (var keyValuePair in configuration.AsEnumerable(true)) -{ - -} - -var appConfigurator = ((IConfigurationBuilder) app.Configuration).ToAppConfigurator(); - -configuration = app.Configuration.GetSection("Logging").GetSection("LogLevel"); -foreach (var keyValuePair in configuration.AsEnumerable(true)) -{ - -} - -app.MapGet("/", () => "Hello World!"); - -app.Run(); diff --git a/TestWebApplication/Properties/launchSettings.json b/TestWebApplication/Properties/launchSettings.json deleted file mode 100644 index ff3ec79..0000000 --- a/TestWebApplication/Properties/launchSettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:56715", - "sslPort": 0 - } - }, - "profiles": { - "TestWebApplication": { - "commandName": "Project", - "dotnetRunMessages": true, - "applicationUrl": "http://localhost:5264", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} diff --git a/TestWebApplication/TestWebApplication.csproj b/TestWebApplication/TestWebApplication.csproj deleted file mode 100644 index 326981f..0000000 --- a/TestWebApplication/TestWebApplication.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - diff --git a/TestWebApplication/appsettings.Development.json b/TestWebApplication/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/TestWebApplication/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/TestWebApplication/appsettings.json b/TestWebApplication/appsettings.json deleted file mode 100644 index 10f68b8..0000000 --- a/TestWebApplication/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/dotnetCampus.Configurations.sln b/dotnetCampus.Configurations.sln index 87df83b..b9842b8 100644 --- a/dotnetCampus.Configurations.sln +++ b/dotnetCampus.Configurations.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.4.33213.308 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29503.13 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{40BD8E4E-62CC-4055-8547-96D733254C87}" ProjectSection(SolutionItems) = preProject @@ -25,8 +25,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Configurations EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests", "tests\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj", "{4AB10B28-5DC6-42FE-8966-5CBC4EB19845}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWebApplication", "TestWebApplication\TestWebApplication.csproj", "{DE36E375-3A89-48C7-A6F8-2A332754BC25}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -70,12 +68,6 @@ Global {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.Release|Any CPU.Build.0 = Release|Any CPU {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.UnitTest|Any CPU.ActiveCfg = Release|Any CPU {4AB10B28-5DC6-42FE-8966-5CBC4EB19845}.UnitTest|Any CPU.Build.0 = Release|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.Release|Any CPU.Build.0 = Release|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.UnitTest|Any CPU.ActiveCfg = Debug|Any CPU - {DE36E375-3A89-48C7-A6F8-2A332754BC25}.UnitTest|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 40149d8983f97f995e02ea8b6d8b11a65e5f347e Mon Sep 17 00:00:00 2001 From: lindexi Date: Fri, 23 Jun 2023 10:49:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Microsoft.Extensions.C?= =?UTF-8?q?onfiguration=20=E4=B8=8E=20AppConfigurator=20=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 ConfigurationManager 无法获取所有配置 原因是 AppConfigurator 对接类里面每次都返回空集合,过滤掉了原有的其他配置提供器提供的配置 --- .../MicrosoftConfigurationExtensions.cs | 8 +++++ ...crosoftExtensionsConfigurationBuildRepo.cs | 4 ++- .../MicrosoftConfigurationExtensions.cs | 32 +++++++++++++++++-- .../appsettings.json | 9 ++++++ ...rosoftExtensionsConfiguration.Tests.csproj | 8 ++++- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/appsettings.json 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 + + +