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 <akahbazi@gmail.com> Co-authored-by: Kahbazi <akahbazi@gmail.com>
This commit is contained in:
parent
aa1646b424
commit
e409c97612
|
|
@ -29,13 +29,18 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
private AggregateException _hostingStartupErrors;
|
private AggregateException _hostingStartupErrors;
|
||||||
private HostingStartupWebHostBuilder _hostingStartupWebHostBuilder;
|
private HostingStartupWebHostBuilder _hostingStartupWebHostBuilder;
|
||||||
|
|
||||||
public GenericWebHostBuilder(IHostBuilder builder)
|
public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
|
||||||
{
|
{
|
||||||
_builder = builder;
|
_builder = builder;
|
||||||
|
var configBuilder = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection();
|
||||||
|
|
||||||
_config = new ConfigurationBuilder()
|
if (!options.SuppressEnvironmentConfiguration)
|
||||||
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
|
{
|
||||||
.Build();
|
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
|
||||||
|
}
|
||||||
|
|
||||||
|
_config = configBuilder.Build();
|
||||||
|
|
||||||
_builder.ConfigureHostConfiguration(config =>
|
_builder.ConfigureHostConfiguration(config =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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 System;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -6,9 +9,37 @@ namespace Microsoft.Extensions.Hosting
|
||||||
{
|
{
|
||||||
public static class GenericHostWebHostBuilderExtensions
|
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)
|
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);
|
configure(webhostBuilder);
|
||||||
builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
|
builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
|
||||||
return builder;
|
return builder;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests.Fakes
|
||||||
|
|
||||||
internal GenericWebHostBuilderWrapper(HostBuilder hostBuilder)
|
internal GenericWebHostBuilderWrapper(HostBuilder hostBuilder)
|
||||||
{
|
{
|
||||||
_builder = new GenericWebHostBuilder(hostBuilder);
|
_builder = new GenericWebHostBuilder(hostBuilder, new WebHostBuilderOptions());
|
||||||
_hostBuilder = hostBuilder;
|
_hostBuilder = hostBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,19 +6,15 @@ using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.ExceptionServices;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Hosting.Fakes;
|
using Microsoft.AspNetCore.Hosting.Fakes;
|
||||||
using Microsoft.AspNetCore.Hosting.Server;
|
using Microsoft.AspNetCore.Hosting.Server;
|
||||||
using Microsoft.AspNetCore.Hosting.Tests.Fakes;
|
using Microsoft.AspNetCore.Hosting.Tests.Fakes;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.WebUtilities;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue