Skip to content
代码片段 群组 项目
未验证 提交 ce733508 编辑于 作者: David Fowler's avatar David Fowler 提交者: GitHub
浏览文件

Add the ConfigurationManager to the IServiceCollection (#36832)

- Add an IConfiguration to the service collection for scenarios where uses end up prematurely building the service provider or sniffing the IServiceCollection for it. We then remove the ConfigurationManager reference it from the final IServiceCollection to avoid cycles in the configuration graph that result in stack overflows (we link the configuration manager to the final configuration that has been built).
- Added tests
上级 77008104
No related branches found
No related tags found
无相关合并请求
...@@ -99,6 +99,8 @@ namespace Microsoft.AspNetCore.Builder ...@@ -99,6 +99,8 @@ namespace Microsoft.AspNetCore.Builder
Logging = new LoggingBuilder(Services); Logging = new LoggingBuilder(Services);
Host = new ConfigureHostBuilder(hostContext, Configuration, Services); Host = new ConfigureHostBuilder(hostContext, Configuration, Services);
WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services); WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services);
Services.AddSingleton<IConfiguration>(Configuration);
} }
/// <summary> /// <summary>
...@@ -171,6 +173,17 @@ namespace Microsoft.AspNetCore.Builder ...@@ -171,6 +173,17 @@ namespace Microsoft.AspNetCore.Builder
// we called ConfigureWebHostDefaults on both the _deferredHostBuilder and _hostBuilder. // we called ConfigureWebHostDefaults on both the _deferredHostBuilder and _hostBuilder.
foreach (var s in _services) foreach (var s in _services)
{ {
// Skip the configuration manager instance we added earlier
// we're already going to wire it up to this new configuration source
// after we've built the application. There's a chance the user manually added
// this as well but we still need to remove it from the final configuration
// to avoid cycles in the configuration graph
if (s.ServiceType == typeof(IConfiguration) &&
s.ImplementationInstance == Configuration)
{
continue;
}
services.Add(s); services.Add(s);
} }
......
...@@ -693,6 +693,39 @@ namespace Microsoft.AspNetCore.Tests ...@@ -693,6 +693,39 @@ namespace Microsoft.AspNetCore.Tests
Assert.Contains("NewHost", options.AllowedHosts); Assert.Contains("NewHost", options.AllowedHosts);
} }
[Fact]
public void CanResolveIConfigurationBeforeBuildingApplication()
{
var builder = WebApplication.CreateBuilder();
var sp = builder.Services.BuildServiceProvider();
var config = sp.GetService<IConfiguration>();
Assert.NotNull(config);
Assert.Same(config, builder.Configuration);
var app = builder.Build();
// These are different
Assert.NotSame(app.Configuration, builder.Configuration);
}
[Fact]
public void ManuallyAddingConfigurationAsServiceWorks()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
var sp = builder.Services.BuildServiceProvider();
var config = sp.GetService<IConfiguration>();
Assert.NotNull(config);
Assert.Same(config, builder.Configuration);
var app = builder.Build();
// These are different
Assert.NotSame(app.Configuration, builder.Configuration);
}
[Fact] [Fact]
public async Task WebApplicationConfiguration_EnablesForwardedHeadersFromConfig() public async Task WebApplicationConfiguration_EnablesForwardedHeadersFromConfig()
{ {
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册