Skip to content

Add the ConfigurationManager to the IServiceCollection (#36832) #36864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2021
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
13 changes: 13 additions & 0 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde
Logging = new LoggingBuilder(Services);
Host = new ConfigureHostBuilder(hostContext, Configuration, Services);
WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services);

Services.AddSingleton<IConfiguration>(Configuration);
}

/// <summary>
Expand Down Expand Up @@ -171,6 +173,17 @@ public WebApplication Build()
// we called ConfigureWebHostDefaults on both the _deferredHostBuilder and _hostBuilder.
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,39 @@ public async Task WebApplicationConfiguration_HostFilterOptionsAreReloadable()
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]
public async Task WebApplicationConfiguration_EnablesForwardedHeadersFromConfig()
{
Expand Down