Skip to content

Commit cbac727

Browse files
committed
Address PR feedback
1 parent 020b18f commit cbac727

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/DefaultBuilder/src/ConfigurationProviderSource.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public bool TryGet(string key, out string value)
6666
}
6767

6868
// 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();
69+
public IEnumerator<IConfigurationProvider> GetEnumerator() => GetUnwrappedEnumerable().GetEnumerator();
7070

71-
IEnumerator IEnumerable.GetEnumerator() => new List<IConfigurationProvider> { _provider }.GetEnumerator();
71+
IEnumerator IEnumerable.GetEnumerator() => GetUnwrappedEnumerable().GetEnumerator();
7272

7373
public override bool Equals(object? obj)
7474
{
@@ -89,6 +89,11 @@ public void Dispose()
8989
{
9090
(_provider as IDisposable)?.Dispose();
9191
}
92+
93+
private IEnumerable<IConfigurationProvider> GetUnwrappedEnumerable()
94+
{
95+
yield return _provider;
96+
}
9297
}
9398
}
9499
}

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ public WebApplication Build()
222222
// Mark the service collection as read-only to prevent future modifications
223223
_services.IsReadOnly = true;
224224

225+
// Resolve both the _hostBuilder's Configuration and builder.Configuration to mark both as resolved within the
226+
// service provider ensuring both will be properly disposed with the provider.
227+
_ = _builtApplication.Services.GetService<IEnumerable<IConfiguration>>();
228+
225229
return _builtApplication;
226230
}
227231

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,8 @@ public async Task WebApplicationDisposesConfigurationProvidersAddedInBuild()
752752
Assert.Equal(1, appConfigSource.ProvidersBuilt);
753753
Assert.Equal(1, hostConfigSource.ProvidersLoaded);
754754
Assert.Equal(1, appConfigSource.ProvidersLoaded);
755-
Assert.Equal(1, hostConfigSource.ProvidersDisposed);
756-
Assert.Equal(1, appConfigSource.ProvidersDisposed);
755+
Assert.True(hostConfigSource.ProvidersDisposed > 0);
756+
Assert.True(appConfigSource.ProvidersDisposed > 0);
757757
}
758758

759759
[Fact]

0 commit comments

Comments
 (0)