Skip to content

Allow suppressing the use of environment variables #25136

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 3 commits into from
Aug 25, 2020
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: 9 additions & 4 deletions src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand Down
33 changes: 32 additions & 1 deletion src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -6,9 +9,37 @@ namespace Microsoft.Extensions.Hosting
{
public static class GenericHostWebHostBuilderExtensions
{
/// <summary>
/// Adds and configures an ASP.NET Core web application.
/// </summary>
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure)
{
var webhostBuilder = new GenericWebHostBuilder(builder);
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

return builder.ConfigureWebHost(configure, _ => { });
}

/// <summary>
/// Adds and configures an ASP.NET Core web application.
/// </summary>
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure, Action<WebHostBuilderOptions> 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<GenericWebHostService>());
return builder;
Expand Down
17 changes: 17 additions & 0 deletions src/Hosting/Hosting/src/WebHostBuilderOptions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Builder options for use with ConfigureWebHost.
/// </summary>
public class WebHostBuilderOptions
{
/// <summary>
/// Indicates if "ASPNETCORE_" prefixed environment variables should be added to configuration.
/// They are added by default.
/// </summary>
public bool SuppressEnvironmentConfiguration { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
41 changes: 41 additions & 0 deletions src/Hosting/Hosting/test/GenericWebHostBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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<IConfiguration>();
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<IConfiguration>();
Assert.Null(config[randomEnvKey]);
Environment.SetEnvironmentVariable("ASPNETCORE_" + randomEnvKey, null);
}
}
}
4 changes: 0 additions & 4 deletions src/Hosting/Hosting/test/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down