Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3007,5 +3007,36 @@ public void BindArraysWithNullAndOtherValues()
Assert.Equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], instance.ByteArray3);
#endif
}

[Fact]
public void TestProvidersOrder()
{
string jsonConfig1 = @"
{
""SimplePoco"": {
""A"": ""Provider1A"",
""B"": ""Provider1B"",
},
}";

// Missing B in the second provider should not override the value from the first provider.
string jsonConfig2 = @"
{
""SimplePoco"": {
""A"": ""Provider2A"",
},
}";

var configuration = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(jsonConfig1)))
.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(jsonConfig2)))
.Build().GetSection("SimplePoco");

SimplePoco? result = configuration.Get<SimplePoco>();

Assert.NotNull(result);
Assert.Equal("Provider2A", result.A); // Value should come from the last provider
Assert.Equal("Provider1B", result.B); // B should not be overridden by the second provider
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ internal static IEnumerable<IConfigurationSection> GetChildrenImplementation(thi

internal static bool TryGetConfiguration(this IConfigurationRoot root, string key, out string? value)
{
foreach (IConfigurationProvider provider in root.Providers)
// common cases Providers is IList<IConfigurationProvider> in ConfigurationRoot
IList<IConfigurationProvider> providers = root.Providers is IList<IConfigurationProvider> list
? list
: root.Providers.ToList();

// ensure looping in the reverse order
for (int i = providers.Count - 1; i >= 0; i--)
{
IConfigurationProvider provider = providers[i];
if (provider.TryGet(key, out value))
{
return true;
Expand Down
Loading