Skip to content

Commit e409c97

Browse files
TratcherKahbazi
andauthored
Allow suppressing the use of environment variables (#25136)
* Allow suppressing the use of environment variables #20328 * Formatting * Update src/Hosting/Hosting/src/WebHostBuilderOptions.cs Co-authored-by: Kahbazi <[email protected]> Co-authored-by: Kahbazi <[email protected]>
1 parent aa1646b commit e409c97

6 files changed

+100
-10
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ internal class GenericWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISuppo
2929
private AggregateException _hostingStartupErrors;
3030
private HostingStartupWebHostBuilder _hostingStartupWebHostBuilder;
3131

32-
public GenericWebHostBuilder(IHostBuilder builder)
32+
public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
3333
{
3434
_builder = builder;
35+
var configBuilder = new ConfigurationBuilder()
36+
.AddInMemoryCollection();
3537

36-
_config = new ConfigurationBuilder()
37-
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
38-
.Build();
38+
if (!options.SuppressEnvironmentConfiguration)
39+
{
40+
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
41+
}
42+
43+
_config = configBuilder.Build();
3944

4045
_builder.ConfigureHostConfiguration(config =>
4146
{

src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
14
using System;
25
using Microsoft.AspNetCore.Hosting;
36
using Microsoft.Extensions.DependencyInjection;
@@ -6,9 +9,37 @@ namespace Microsoft.Extensions.Hosting
69
{
710
public static class GenericHostWebHostBuilderExtensions
811
{
12+
/// <summary>
13+
/// Adds and configures an ASP.NET Core web application.
14+
/// </summary>
915
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure)
1016
{
11-
var webhostBuilder = new GenericWebHostBuilder(builder);
17+
if (configure is null)
18+
{
19+
throw new ArgumentNullException(nameof(configure));
20+
}
21+
22+
return builder.ConfigureWebHost(configure, _ => { });
23+
}
24+
25+
/// <summary>
26+
/// Adds and configures an ASP.NET Core web application.
27+
/// </summary>
28+
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure, Action<WebHostBuilderOptions> configureWebHostBuilder)
29+
{
30+
if (configure is null)
31+
{
32+
throw new ArgumentNullException(nameof(configure));
33+
}
34+
35+
if (configureWebHostBuilder is null)
36+
{
37+
throw new ArgumentNullException(nameof(configureWebHostBuilder));
38+
}
39+
40+
var webHostBuilderOptions = new WebHostBuilderOptions();
41+
configureWebHostBuilder(webHostBuilderOptions);
42+
var webhostBuilder = new GenericWebHostBuilder(builder, webHostBuilderOptions);
1243
configure(webhostBuilder);
1344
builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
1445
return builder;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.Extensions.Hosting
5+
{
6+
/// <summary>
7+
/// Builder options for use with ConfigureWebHost.
8+
/// </summary>
9+
public class WebHostBuilderOptions
10+
{
11+
/// <summary>
12+
/// Indicates if "ASPNETCORE_" prefixed environment variables should be added to configuration.
13+
/// They are added by default.
14+
/// </summary>
15+
public bool SuppressEnvironmentConfiguration { get; set; } = false;
16+
}
17+
}

src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GenericWebHostBuilderWrapper : IWebHostBuilder, ISupportsStartup, I
1616

1717
internal GenericWebHostBuilderWrapper(HostBuilder hostBuilder)
1818
{
19-
_builder = new GenericWebHostBuilder(hostBuilder);
19+
_builder = new GenericWebHostBuilder(hostBuilder, new WebHostBuilderOptions());
2020
_hostBuilder = hostBuilder;
2121
}
2222

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNetCore.Hosting
11+
{
12+
// Most functionality is covered by WebHostBuilderTests for compat. Only GenericHost specific functionality is covered here.
13+
public class GenericWebHostBuilderTests
14+
{
15+
[Fact]
16+
public void ReadsAspNetCoreEnvironmentVariables()
17+
{
18+
var randomEnvKey = Guid.NewGuid().ToString();
19+
Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, "true");
20+
using var host = new HostBuilder()
21+
.ConfigureWebHost(_ => { })
22+
.Build();
23+
var config = host.Services.GetRequiredService<IConfiguration>();
24+
Assert.Equal("true", config[randomEnvKey]);
25+
Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, null);
26+
}
27+
28+
[Fact]
29+
public void CanSuppressAspNetCoreEnvironmentVariables()
30+
{
31+
var randomEnvKey = Guid.NewGuid().ToString();
32+
Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, "true");
33+
using var host = new HostBuilder()
34+
.ConfigureWebHost(_ => { }, webHostBulderOptions => { webHostBulderOptions.SuppressEnvironmentConfiguration = true; })
35+
.Build();
36+
var config = host.Services.GetRequiredService<IConfiguration>();
37+
Assert.Null(config[randomEnvKey]);
38+
Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, null);
39+
}
40+
}
41+
}

src/Hosting/Hosting/test/WebHostBuilderTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9-
using System.Runtime.ExceptionServices;
109
using System.Threading;
1110
using System.Threading.Tasks;
12-
using System.Web;
1311
using Microsoft.AspNetCore.Builder;
1412
using Microsoft.AspNetCore.Hosting;
1513
using Microsoft.AspNetCore.Hosting.Fakes;
1614
using Microsoft.AspNetCore.Hosting.Server;
1715
using Microsoft.AspNetCore.Hosting.Tests.Fakes;
1816
using Microsoft.AspNetCore.Http;
19-
using Microsoft.AspNetCore.Http.Extensions;
2017
using Microsoft.AspNetCore.Http.Features;
21-
using Microsoft.AspNetCore.WebUtilities;
2218
using Microsoft.Extensions.Configuration;
2319
using Microsoft.Extensions.DependencyInjection;
2420
using Microsoft.Extensions.Hosting;

0 commit comments

Comments
 (0)