Replace EnvironmentName with Environments #7733 (#7899)

This commit is contained in:
Chris Ross 2019-02-25 08:13:48 -08:00 committed by GitHub
parent 1b27c9905c
commit ff79e9bda5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 26 deletions

View File

@ -6,10 +6,11 @@ namespace Microsoft.AspNetCore.Hosting
/// <summary> /// <summary>
/// Commonly used environment names. /// Commonly used environment names.
/// </summary> /// </summary>
[System.Obsolete("This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.Extensions.Hosting.Environments.", error: false)]
public static class EnvironmentName public static class EnvironmentName
{ {
public static readonly string Development = "Development"; public static readonly string Development = "Development";
public static readonly string Staging = "Staging"; public static readonly string Staging = "Staging";
public static readonly string Production = "Production"; public static readonly string Production = "Production";
} }
} }

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
public class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment, IWebHostEnvironment public class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment, IWebHostEnvironment
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
{ {
public string EnvironmentName { get; set; } = Hosting.EnvironmentName.Production; public string EnvironmentName { get; set; } = Extensions.Hosting.Environments.Production;
public string ApplicationName { get; set; } public string ApplicationName { get; set; }

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Hosting.Tests.Internal; using Microsoft.AspNetCore.Hosting.Tests.Internal;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Xunit; using Xunit;
@ -510,7 +511,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>(); serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -526,7 +527,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>(); serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), Environments.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -542,13 +543,13 @@ namespace Microsoft.AspNetCore.Hosting.Tests
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>(); serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), EnvironmentName.Production); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), Environments.Production);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
Assert.IsType<MyContainer>(app.ApplicationServices); Assert.IsType<MyContainer>(app.ApplicationServices);
Assert.Equal(((MyContainer)app.ApplicationServices).Environment, EnvironmentName.Production); Assert.Equal(((MyContainer)app.ApplicationServices).Environment, Environments.Production);
} }
[Fact] [Fact]
@ -557,7 +558,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
Assert.Throws<InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection)); Assert.Throws<InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection));
} }
@ -568,7 +569,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development)); Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), Environments.Development));
} }
[Fact] [Fact]
@ -578,7 +579,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>(); serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupWithOverloads), EnvironmentName.Development)); Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupWithOverloads), Environments.Development));
} }
[Fact] [Fact]
@ -588,7 +589,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyBadContainerFactory>(); serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyBadContainerFactory>();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -629,12 +630,12 @@ namespace Microsoft.AspNetCore.Hosting.Tests
public void ConfigureDevelopmentContainer(MyContainer container) public void ConfigureDevelopmentContainer(MyContainer container)
{ {
container.Environment = EnvironmentName.Development; container.Environment = Environments.Development;
} }
public void ConfigureProductionContainer(MyContainer container) public void ConfigureProductionContainer(MyContainer container)
{ {
container.Environment = EnvironmentName.Production; container.Environment = Environments.Production;
} }
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)

View File

@ -4,6 +4,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Hosting.Tests namespace Microsoft.AspNetCore.Hosting.Tests
@ -18,7 +19,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{ WebHostDefaults.WebRootKey, "wwwroot"}, { WebHostDefaults.WebRootKey, "wwwroot"},
{ WebHostDefaults.ApplicationKey, "MyProjectReference"}, { WebHostDefaults.ApplicationKey, "MyProjectReference"},
{ WebHostDefaults.StartupAssemblyKey, "MyProjectReference" }, { WebHostDefaults.StartupAssemblyKey, "MyProjectReference" },
{ WebHostDefaults.EnvironmentKey, EnvironmentName.Development}, { WebHostDefaults.EnvironmentKey, Environments.Development},
{ WebHostDefaults.DetailedErrorsKey, "true"}, { WebHostDefaults.DetailedErrorsKey, "true"},
{ WebHostDefaults.CaptureStartupErrorsKey, "true" }, { WebHostDefaults.CaptureStartupErrorsKey, "true" },
{ WebHostDefaults.SuppressStatusMessagesKey, "true" } { WebHostDefaults.SuppressStatusMessagesKey, "true" }
@ -29,7 +30,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
Assert.Equal("wwwroot", config.WebRoot); Assert.Equal("wwwroot", config.WebRoot);
Assert.Equal("MyProjectReference", config.ApplicationName); Assert.Equal("MyProjectReference", config.ApplicationName);
Assert.Equal("MyProjectReference", config.StartupAssembly); Assert.Equal("MyProjectReference", config.StartupAssembly);
Assert.Equal(EnvironmentName.Development, config.Environment); Assert.Equal(Environments.Development, config.Environment);
Assert.True(config.CaptureStartupErrors); Assert.True(config.CaptureStartupErrors);
Assert.True(config.DetailedErrors); Assert.True(config.DetailedErrors);
Assert.True(config.SuppressStatusMessages); Assert.True(config.SuppressStatusMessages);
@ -38,10 +39,10 @@ namespace Microsoft.AspNetCore.Hosting.Tests
[Fact] [Fact]
public void ReadsOldEnvKey() public void ReadsOldEnvKey()
{ {
var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", EnvironmentName.Development } }; var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", Environments.Development } };
var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build());
Assert.Equal(EnvironmentName.Development, config.Environment); Assert.Equal(Environments.Development, config.Environment);
} }
[Theory] [Theory]

View File

@ -812,8 +812,8 @@ namespace Microsoft.AspNetCore.Hosting
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>(); var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>();
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal(EnvironmentName.Production, env.EnvironmentName); Assert.Equal(Environments.Production, env.EnvironmentName);
Assert.Equal(EnvironmentName.Production, env2.EnvironmentName); Assert.Equal(Environments.Production, env2.EnvironmentName);
} }
} }
@ -822,7 +822,7 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var vals = new Dictionary<string, string> var vals = new Dictionary<string, string>
{ {
{ "Environment", EnvironmentName.Staging } { "Environment", Environments.Staging }
}; };
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
@ -835,8 +835,8 @@ namespace Microsoft.AspNetCore.Hosting
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>(); var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>();
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
Assert.Equal(EnvironmentName.Staging, env.EnvironmentName); Assert.Equal(Environments.Staging, env.EnvironmentName);
Assert.Equal(EnvironmentName.Staging, env.EnvironmentName); Assert.Equal(Environments.Staging, env.EnvironmentName);
} }
} }
@ -873,7 +873,7 @@ namespace Microsoft.AspNetCore.Hosting
{ {
await host.StartAsync(); await host.StartAsync();
var env = host.Services.GetRequiredService<IHostEnvironment>(); var env = host.Services.GetRequiredService<IHostEnvironment>();
Assert.True(env.IsEnvironment(EnvironmentName.Production)); Assert.True(env.IsEnvironment(Environments.Production));
Assert.True(env.IsEnvironment("producTion")); Assert.True(env.IsEnvironment("producTion"));
} }
} }

View File

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
// Note that this sample will not run. It is only here to illustrate usage patterns. // Note that this sample will not run. It is only here to illustrate usage patterns.
@ -25,7 +26,7 @@ namespace SampleStartups
.UseKestrel() .UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory .UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902") .UseUrls("http://*:1000", "https://*:902")
.UseEnvironment(EnvironmentName.Development) .UseEnvironment(Environments.Development)
.UseWebRoot("public") .UseWebRoot("public")
.ConfigureServices(services => .ConfigureServices(services =>
{ {

View File

@ -13,7 +13,6 @@ using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel; using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using EnvironmentName = Microsoft.Extensions.Hosting.EnvironmentName;
namespace Microsoft.AspNetCore.Mvc.Testing namespace Microsoft.AspNetCore.Mvc.Testing
{ {
@ -307,7 +306,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
var hostBuilder = HostFactoryResolver.ResolveHostBuilderFactory<IHostBuilder>(typeof(TEntryPoint).Assembly)?.Invoke(Array.Empty<string>()); var hostBuilder = HostFactoryResolver.ResolveHostBuilderFactory<IHostBuilder>(typeof(TEntryPoint).Assembly)?.Invoke(Array.Empty<string>());
if (hostBuilder != null) if (hostBuilder != null)
{ {
hostBuilder.UseEnvironment(EnvironmentName.Development); hostBuilder.UseEnvironment(Environments.Development);
} }
return hostBuilder; return hostBuilder;
} }
@ -336,7 +335,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
} }
else else
{ {
return builder.UseEnvironment(EnvironmentName.Development); return builder.UseEnvironment(Environments.Development);
} }
} }