Skip to content

Commit

Permalink
Merge pull request #67 from dotnet-campus/t/lindexi/Config
Browse files Browse the repository at this point in the history
修复 Microsoft.Extensions.Configuration 与 AppConfigurator 冲突
  • Loading branch information
lindexi authored Jun 24, 2023
2 parents 4fd6784 + 40149d8 commit bafa8d7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ public static IAppConfigurator ToAppConfigurator(this IConfiguration configurati
/// <returns></returns>
public static IAppConfigurator ToAppConfigurator(this IConfigurationBuilder configuration) =>
new MicrosoftExtensionsConfigurationBuildRepo(configuration).CreateAppConfigurator();

/// <inheritdoc cref="ToAppConfigurator(IConfigurationBuilder)"/>
public static IAppConfigurator ToAppConfigurator<T>(this T configuration)
where T : IConfiguration, IConfigurationBuilder
{
IConfigurationBuilder configurationBuilder = configuration;
return configurationBuilder.ToAppConfigurator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public void Load()

public IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath)
{
return Array.Empty<string>();
// 这里如果返回空集合,将会清空原有的配置内容。这个函数的作用是用来进行过滤和追加两个合一起,底层框架这样设计是为了性能考虑。在一个配置管理里面,是由多个 IConfigurationProvider 组成,而多个 IConfigurationProvider 之间,需要有相互影响。在获取所有的 GetChildKeys 时候,假定每个 IConfigurationProvider 都需要追加自身的,那传入 IEnumerable 类型,用于追加是最省资源的。而有些 IConfigurationProvider 之间提供了相同的 Key 的配置,但是有些 IConfigurationProvider 期望覆盖,有些期望不覆盖,于是就通过 earlierKeys 即可用来实现过滤判断,每个不同的 IConfigurationProvider 可以有自己的策略,对先加入的 IConfigurationProvider 返回的 GetChildKeys 进行处理
// return Array.Empty<string>();
return earlierKeys;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down Expand Up @@ -42,9 +70,9 @@ public void ConfigurationToAppConfigurator()
const string value = "doubi";
var memoryConfigurationSource = new MemoryConfigurationSource()
{
InitialData = new List<KeyValuePair<string, string>>()
InitialData = new List<KeyValuePair<string, string?>>()
{
new KeyValuePair<string, string>(key, value)
new KeyValuePair<string, string?>(key, value)
}
};
IConfigurationProvider configurationProvider=new MemoryConfigurationProvider(memoryConfigurationSource);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
Expand All @@ -24,4 +24,10 @@
<ProjectReference Include="..\..\src\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration\dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit bafa8d7

Please sign in to comment.