diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
index 9f97f03c4ef6..96f886e47446 100644
--- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
+++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
@@ -29,13 +29,18 @@ internal class GenericWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISuppo
private AggregateException _hostingStartupErrors;
private HostingStartupWebHostBuilder _hostingStartupWebHostBuilder;
- public GenericWebHostBuilder(IHostBuilder builder)
+ public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
{
_builder = builder;
+ var configBuilder = new ConfigurationBuilder()
+ .AddInMemoryCollection();
- _config = new ConfigurationBuilder()
- .AddEnvironmentVariables(prefix: "ASPNETCORE_")
- .Build();
+ if (!options.SuppressEnvironmentConfiguration)
+ {
+ configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
+ }
+
+ _config = configBuilder.Build();
_builder.ConfigureHostConfiguration(config =>
{
diff --git a/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs b/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs
index feb6da2ca4ba..4899a8caf2b5 100644
--- a/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs
+++ b/src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs
@@ -1,3 +1,6 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
@@ -6,9 +9,37 @@ namespace Microsoft.Extensions.Hosting
{
public static class GenericHostWebHostBuilderExtensions
{
+ ///
+ /// Adds and configures an ASP.NET Core web application.
+ ///
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action configure)
{
- var webhostBuilder = new GenericWebHostBuilder(builder);
+ if (configure is null)
+ {
+ throw new ArgumentNullException(nameof(configure));
+ }
+
+ return builder.ConfigureWebHost(configure, _ => { });
+ }
+
+ ///
+ /// Adds and configures an ASP.NET Core web application.
+ ///
+ public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action configure, Action configureWebHostBuilder)
+ {
+ if (configure is null)
+ {
+ throw new ArgumentNullException(nameof(configure));
+ }
+
+ if (configureWebHostBuilder is null)
+ {
+ throw new ArgumentNullException(nameof(configureWebHostBuilder));
+ }
+
+ var webHostBuilderOptions = new WebHostBuilderOptions();
+ configureWebHostBuilder(webHostBuilderOptions);
+ var webhostBuilder = new GenericWebHostBuilder(builder, webHostBuilderOptions);
configure(webhostBuilder);
builder.ConfigureServices((context, services) => services.AddHostedService());
return builder;
diff --git a/src/Hosting/Hosting/src/WebHostBuilderOptions.cs b/src/Hosting/Hosting/src/WebHostBuilderOptions.cs
new file mode 100644
index 000000000000..7fe6caef09e3
--- /dev/null
+++ b/src/Hosting/Hosting/src/WebHostBuilderOptions.cs
@@ -0,0 +1,17 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+namespace Microsoft.Extensions.Hosting
+{
+ ///
+ /// Builder options for use with ConfigureWebHost.
+ ///
+ public class WebHostBuilderOptions
+ {
+ ///
+ /// Indicates if "ASPNETCORE_" prefixed environment variables should be added to configuration.
+ /// They are added by default.
+ ///
+ public bool SuppressEnvironmentConfiguration { get; set; } = false;
+ }
+}
diff --git a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs
index b0bf122d1b33..a8660bedd57e 100644
--- a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs
+++ b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs
@@ -16,7 +16,7 @@ public class GenericWebHostBuilderWrapper : IWebHostBuilder, ISupportsStartup, I
internal GenericWebHostBuilderWrapper(HostBuilder hostBuilder)
{
- _builder = new GenericWebHostBuilder(hostBuilder);
+ _builder = new GenericWebHostBuilder(hostBuilder, new WebHostBuilderOptions());
_hostBuilder = hostBuilder;
}
diff --git a/src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs b/src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs
new file mode 100644
index 000000000000..7ac55b1aecb3
--- /dev/null
+++ b/src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs
@@ -0,0 +1,41 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Hosting
+{
+ // Most functionality is covered by WebHostBuilderTests for compat. Only GenericHost specific functionality is covered here.
+ public class GenericWebHostBuilderTests
+ {
+ [Fact]
+ public void ReadsAspNetCoreEnvironmentVariables()
+ {
+ var randomEnvKey = Guid.NewGuid().ToString();
+ Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, "true");
+ using var host = new HostBuilder()
+ .ConfigureWebHost(_ => { })
+ .Build();
+ var config = host.Services.GetRequiredService();
+ Assert.Equal("true", config[randomEnvKey]);
+ Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, null);
+ }
+
+ [Fact]
+ public void CanSuppressAspNetCoreEnvironmentVariables()
+ {
+ var randomEnvKey = Guid.NewGuid().ToString();
+ Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, "true");
+ using var host = new HostBuilder()
+ .ConfigureWebHost(_ => { }, webHostBulderOptions => { webHostBulderOptions.SuppressEnvironmentConfiguration = true; })
+ .Build();
+ var config = host.Services.GetRequiredService();
+ Assert.Null(config[randomEnvKey]);
+ Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, null);
+ }
+ }
+}
diff --git a/src/Hosting/Hosting/test/WebHostBuilderTests.cs b/src/Hosting/Hosting/test/WebHostBuilderTests.cs
index d6b9a5f1a6dc..37ccbe5f21e3 100644
--- a/src/Hosting/Hosting/test/WebHostBuilderTests.cs
+++ b/src/Hosting/Hosting/test/WebHostBuilderTests.cs
@@ -6,19 +6,15 @@
using System.IO;
using System.Linq;
using System.Reflection;
-using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
-using System.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Fakes;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Tests.Fakes;
using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Features;
-using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;