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()
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902")
.UseEnvironment("Development")
.UseEnvironment(EnvironmentName.Development)
.UseWebRoot("public")
.ConfigureServices(services =>
{

View File

@ -12,10 +12,10 @@ namespace Microsoft.AspNetCore.Hosting
public static class HostingEnvironmentExtensions
{
/// <summary>
/// Checks if the current hosting environment name is "Development".
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Development"/>.
/// </summary>
/// <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)
{
if (hostingEnvironment == null)
@ -27,10 +27,10 @@ namespace Microsoft.AspNetCore.Hosting
}
/// <summary>
/// Checks if the current hosting environment name is "Staging".
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Staging"/>.
/// </summary>
/// <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)
{
if (hostingEnvironment == null)
@ -42,10 +42,10 @@ namespace Microsoft.AspNetCore.Hosting
}
/// <summary>
/// Checks if the current hosting environment name is "Production".
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Production"/>.
/// </summary>
/// <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)
{
if (hostingEnvironment == null)

View File

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

View File

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

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{ "webroot", "wwwroot"},
{ "applicationName", "MyProjectReference"},
{ "startupAssembly", "MyProjectReference" },
{ "environment", "Development"},
{ "environment", EnvironmentName.Development},
{ "detailederrors", "true"},
{ "captureStartupErrors", "true" }
};
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
Assert.Equal("wwwroot", config.WebRoot);
Assert.Equal("MyProjectReference", config.ApplicationName);
Assert.Equal("MyProjectReference", config.StartupAssembly);
Assert.Equal("Development", config.Environment);
Assert.Equal(EnvironmentName.Development, config.Environment);
Assert.True(config.CaptureStartupErrors);
Assert.True(config.DetailedErrors);
}
@ -36,10 +36,10 @@ namespace Microsoft.AspNetCore.Hosting.Tests
[Fact]
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());
Assert.Equal("Development", config.Environment);
Assert.Equal(EnvironmentName.Development, config.Environment);
}
[Theory]

View File

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