Skip to content
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

[release/6.0] Make UseUrls() override default hosting config #39836

Merged
merged 1 commit into from
Feb 3, 2022
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
21 changes: 17 additions & 4 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class WebApplicationBuilder
private readonly BootstrapHostBuilder _bootstrapHostBuilder;
private readonly WebApplicationServiceCollection _services = new();
private readonly List<KeyValuePair<string, string>> _hostConfigurationValues;
private readonly ConfigurationManager _hostConfigurationManager = new();

private WebApplication? _builtApplication;

Expand Down Expand Up @@ -76,6 +77,8 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde
});

Configuration = new();
// This is chained as the first configuration source in Configuration so host config can be added later without overriding app config.
Configuration.AddConfiguration(_hostConfigurationManager, shouldDisposeConfiguration: true);

// Collect the hosted services separately since we want those to run after the user's hosted services
_services.TrackHostedServices = true;
Expand Down Expand Up @@ -194,22 +197,32 @@ public WebApplication Build()
// to the new one. This allows code that has references to the service collection to still function.
_services.InnerCollection = services;

// Keep any configuration sources added before the TrackingChainedConfigurationSource (namely host configuration from _hostConfigurationValues)
// from overriding config values set via Configuration by inserting them at beginning using _hostConfigurationValues.
var beforeChainedConfig = true;
var hostBuilderProviders = ((IConfigurationRoot)context.Configuration).Providers;

if (!hostBuilderProviders.Contains(chainedConfigSource.BuiltProvider))
{
// Something removed the _hostBuilder's TrackingChainedConfigurationSource pointing back to the ConfigurationManager.
// This is likely a test using WebApplicationFactory. Replicate the effect by clearing the ConfingurationManager sources.
((IConfigurationBuilder)Configuration).Sources.Clear();
beforeChainedConfig = false;
}

// Make builder.Configuration match the final configuration. To do that, we add the additional
// providers in the inner _hostBuilders's Configuration to the ConfigurationManager.
// Make the ConfigurationManager match the final _hostBuilder's configuration. To do that, we add the additional providers
// to the inner _hostBuilders's configuration to the ConfigurationManager. We wrap the existing provider in a
// configuration source to avoid rebuilding or reloading the already added configuration sources.
foreach (var provider in hostBuilderProviders)
{
if (!ReferenceEquals(provider, chainedConfigSource.BuiltProvider))
if (ReferenceEquals(provider, chainedConfigSource.BuiltProvider))
{
((IConfigurationBuilder)Configuration).Add(new ConfigurationProviderSource(provider));
beforeChainedConfig = false;
}
else
{
IConfigurationBuilder configBuilder = beforeChainedConfig ? _hostConfigurationManager : Configuration;
configBuilder.Add(new ConfigurationProviderSource(provider));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ public async Task WebApplicationRunUrls_OverridesIServerAddressesFeature()
await runTask;
}

[Fact]
public async Task WebApplicationWebHostUseUrls_OverridesDefaultHostingConfiguration()
{
var builder = new WebApplicationBuilder(new(), bootstrapBuilder =>
{
bootstrapBuilder.ConfigureHostConfiguration(configBuilder =>
{
configBuilder.AddInMemoryCollection(new Dictionary<string, string>
{
[WebHostDefaults.ServerUrlsKey] = "http://localhost:5000",
});
});
});

builder.WebHost.UseUrls("http://localhost:5001");

var urls = new List<string>();
var server = new MockAddressesServer(urls);
builder.Services.AddSingleton<IServer>(server);
await using var app = builder.Build();

await app.StartAsync();

var url = Assert.Single(urls);
Assert.Equal("http://localhost:5001", url);
}

[Fact]
public async Task WebApplicationUrls_ThrowsInvalidOperationExceptionIfThereIsNoIServerAddressesFeature()
{
Expand Down