Use EnvironmentName instead of magic string (#1065)

This commit is contained in:
Hisham Bin Ateya 2017-05-11 01:20:40 +03:00 committed by Chris R
parent f8779ee377
commit cf23aedb55
6 changed files with 23 additions and 24 deletions

View File

@ -26,7 +26,7 @@ namespace SampleStartups
.UseFakeServer() .UseFakeServer()
.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("Development") .UseEnvironment(EnvironmentName.Development)
.UseWebRoot("public") .UseWebRoot("public")
.ConfigureServices(services => .ConfigureServices(services =>
{ {

View File

@ -12,10 +12,10 @@ namespace Microsoft.AspNetCore.Hosting
public static class HostingEnvironmentExtensions public static class HostingEnvironmentExtensions
{ {
/// <summary> /// <summary>
/// Checks if the current hosting environment name is "Development". /// Checks if the current hosting environment name is <see cref="EnvironmentName.Development"/>.
/// </summary> /// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param> /// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param>
/// <returns>True if the environment name is "Development", otherwise false.</returns> /// <returns>True if the environment name is <see cref="EnvironmentName.Development"/>, otherwise false.</returns>
public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment) public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{ {
if (hostingEnvironment == null) if (hostingEnvironment == null)
@ -27,10 +27,10 @@ namespace Microsoft.AspNetCore.Hosting
} }
/// <summary> /// <summary>
/// Checks if the current hosting environment name is "Staging". /// Checks if the current hosting environment name is <see cref="EnvironmentName.Staging"/>.
/// </summary> /// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param> /// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param>
/// <returns>True if the environment name is "Staging", otherwise false.</returns> /// <returns>True if the environment name is <see cref="EnvironmentName.Staging"/>, otherwise false.</returns>
public static bool IsStaging(this IHostingEnvironment hostingEnvironment) public static bool IsStaging(this IHostingEnvironment hostingEnvironment)
{ {
if (hostingEnvironment == null) if (hostingEnvironment == null)
@ -42,10 +42,10 @@ namespace Microsoft.AspNetCore.Hosting
} }
/// <summary> /// <summary>
/// Checks if the current hosting environment name is "Production". /// Checks if the current hosting environment name is <see cref="EnvironmentName.Production"/>.
/// </summary> /// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param> /// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param>
/// <returns>True if the environment name is "Production", otherwise false.</returns> /// <returns>True if the environment name is <see cref="EnvironmentName.Production"/>, otherwise false.</returns>
public static bool IsProduction(this IHostingEnvironment hostingEnvironment) public static bool IsProduction(this IHostingEnvironment hostingEnvironment)
{ {
if (hostingEnvironment == null) if (hostingEnvironment == null)

View File

@ -15,7 +15,6 @@ namespace Microsoft.AspNetCore.TestHost
{ {
public class TestServer : IServer public class TestServer : IServer
{ {
private const string DefaultEnvironmentName = "Development";
private const string ServerName = nameof(TestServer); private const string ServerName = nameof(TestServer);
private IWebHost _hostInstance; private IWebHost _hostInstance;
private bool _disposed = false; private bool _disposed = false;

View File

@ -195,7 +195,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), "Development"); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -211,7 +211,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), "Development"); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -227,13 +227,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), "Production"); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), EnvironmentName.Production);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
Assert.IsType(typeof(MyContainer), app.ApplicationServices); Assert.IsType(typeof(MyContainer), app.ApplicationServices);
Assert.Equal(((MyContainer)app.ApplicationServices).Environment, "Production"); Assert.Equal(((MyContainer)app.ApplicationServices).Environment, EnvironmentName.Production);
} }
[Fact] [Fact]
@ -242,7 +242,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), "Development"); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
Assert.Throws<InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection)); Assert.Throws<InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection));
} }
@ -253,7 +253,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), "Development")); Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development));
} }
[Fact] [Fact]
@ -263,7 +263,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), "Development")); Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupWithOverloads), EnvironmentName.Development));
} }
[Fact] [Fact]
@ -273,7 +273,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), "Development"); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
var app = new ApplicationBuilder(services); var app = new ApplicationBuilder(services);
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@ -314,12 +314,12 @@ namespace Microsoft.AspNetCore.Hosting.Tests
public void ConfigureDevelopmentContainer(MyContainer container) public void ConfigureDevelopmentContainer(MyContainer container)
{ {
container.Environment = "Development"; container.Environment = EnvironmentName.Development;
} }
public void ConfigureProductionContainer(MyContainer container) public void ConfigureProductionContainer(MyContainer container)
{ {
container.Environment = "Production"; container.Environment = EnvironmentName.Production;
} }
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{ "webroot", "wwwroot"}, { "webroot", "wwwroot"},
{ "applicationName", "MyProjectReference"}, { "applicationName", "MyProjectReference"},
{ "startupAssembly", "MyProjectReference" }, { "startupAssembly", "MyProjectReference" },
{ "environment", "Development"}, { "environment", EnvironmentName.Development},
{ "detailederrors", "true"}, { "detailederrors", "true"},
{ "captureStartupErrors", "true" } { "captureStartupErrors", "true" }
}; };
@ -28,7 +28,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("Development", config.Environment); Assert.Equal(EnvironmentName.Development, config.Environment);
Assert.True(config.CaptureStartupErrors); Assert.True(config.CaptureStartupErrors);
Assert.True(config.DetailedErrors); Assert.True(config.DetailedErrors);
} }
@ -36,10 +36,10 @@ namespace Microsoft.AspNetCore.Hosting.Tests
[Fact] [Fact]
public void ReadsOldEnvKey() public void ReadsOldEnvKey()
{ {
var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", "Development" } }; var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", EnvironmentName.Development } };
var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build());
Assert.Equal("Development", config.Environment); Assert.Equal(EnvironmentName.Development, config.Environment);
} }
[Theory] [Theory]

View File

@ -662,7 +662,7 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var vals = new Dictionary<string, string> var vals = new Dictionary<string, string>
{ {
{ "Environment", "Staging" } { "Environment", EnvironmentName.Staging }
}; };
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
@ -672,7 +672,7 @@ namespace Microsoft.AspNetCore.Hosting
using (var host = CreateBuilder(config).UseFakeServer().Build()) using (var host = CreateBuilder(config).UseFakeServer().Build())
{ {
var env = host.Services.GetService<IHostingEnvironment>(); var env = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Staging", env.EnvironmentName); Assert.Equal(EnvironmentName.Staging, env.EnvironmentName);
} }
} }