diff options
author | Stephen Halter <halter73@gmail.com> | 2022-01-28 08:25:52 +0300 |
---|---|---|
committer | Stephen Halter <halter73@gmail.com> | 2022-01-28 08:25:52 +0300 |
commit | 8c4a266626570c15f0ef0bec01ced2824fed5d49 (patch) | |
tree | 442ed9fc8dfc7176f022585c9d4ae08a3c09cd35 | |
parent | 467573dc220de7a1aa42744ff24c2c57e72e4aba (diff) |
Make UseUrls() override default hosting confighalter73/38185
-rw-r--r-- | src/DefaultBuilder/src/WebApplicationBuilder.cs | 18 | ||||
-rw-r--r-- | src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs | 28 |
2 files changed, 43 insertions, 3 deletions
diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index e558cfc3d9..2f55b808a6 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -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; @@ -76,6 +77,8 @@ public sealed class WebApplicationBuilder }); 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); // Collect the hosted services separately since we want those to run after the user's hosted services _services.TrackHostedServices = true; @@ -194,6 +197,9 @@ public sealed class WebApplicationBuilder // 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)) @@ -201,17 +207,23 @@ public sealed class WebApplicationBuilder // 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 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 rebulding or reloading the already added configuration sources. + // configuration source to avoid rebuilding or reloading the already added configuration sources. foreach (var provider in hostBuilderProviders) { // Avoid creating a circular reference to the ConfigurationManager via the chained configuration source. - 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)); } } }); diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs index 281bd8cc5d..1af6944f4a 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs @@ -130,6 +130,34 @@ public class WebApplicationTests } [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", + }); + }); + }); + + var urls = new List<string>(); + var server = new MockAddressesServer(urls); + builder.Services.AddSingleton<IServer>(server); + + builder.WebHost.UseUrls("http://localhost:5001"); + + 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() { var builder = WebApplication.CreateBuilder(); |