Skip to content

Commit 020b18f

Browse files
committed
Allow access to original IConfigurationProvider
1 parent 0ae7eb7 commit 020b18f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/DefaultBuilder/src/ConfigurationProviderSource.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.Primitives;
67

@@ -22,7 +23,7 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)
2223

2324
// These providers have already been loaded, so no need to reload initially.
2425
// Otherwise, providers that cannot be reloaded like StreamConfigurationProviders will fail.
25-
private sealed class IgnoreFirstLoadConfigurationProvider : IConfigurationProvider, IDisposable
26+
private sealed class IgnoreFirstLoadConfigurationProvider : IConfigurationProvider, IEnumerable<IConfigurationProvider>, IDisposable
2627
{
2728
private readonly IConfigurationProvider _provider;
2829

@@ -64,6 +65,11 @@ public bool TryGet(string key, out string value)
6465
return _provider.TryGet(key, out value);
6566
}
6667

68+
// Provide access to the original IConfigurationProvider via a single-element IEnumerable to code that goes out of its way to look for it.
69+
public IEnumerator<IConfigurationProvider> GetEnumerator() => new List<IConfigurationProvider> { _provider }.GetEnumerator();
70+
71+
IEnumerator IEnumerable.GetEnumerator() => new List<IConfigurationProvider> { _provider }.GetEnumerator();
72+
6773
public override bool Equals(object? obj)
6874
{
6975
return _provider.Equals(obj);

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,24 @@ public async Task WebApplicationDisposesConfigurationProvidersAddedInBuild()
756756
Assert.Equal(1, appConfigSource.ProvidersDisposed);
757757
}
758758

759+
[Fact]
760+
public async Task WebApplicationMakesOriginalConfigurationProvidersAddedInBuildAccessable()
761+
{
762+
// This mimics what WebApplicationFactory<T> does and runs configure
763+
// services callbacks
764+
using var listener = new HostingListener(hostBuilder =>
765+
{
766+
hostBuilder.ConfigureAppConfiguration(config => config.Add(new RandomConfigurationSource()));
767+
});
768+
769+
var builder = WebApplication.CreateBuilder();
770+
await using var app = builder.Build();
771+
772+
var wrappedProviders = ((IConfigurationRoot)app.Configuration).Providers.OfType<IEnumerable<IConfigurationProvider>>();
773+
var unwrappedProviders = wrappedProviders.Select(p => Assert.Single(p));
774+
Assert.Single(unwrappedProviders.OfType<RandomConfigurationProvider>());
775+
}
776+
759777
[Fact]
760778
public void WebApplicationBuilderHostProperties_IsCaseSensitive()
761779
{

0 commit comments

Comments
 (0)