Skip to content

Commit ff79e9b

Browse files
authored
Replace EnvironmentName with Environments #7733 (#7899)
1 parent 1b27c99 commit ff79e9b

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

src/Hosting/Abstractions/src/EnvironmentName.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace Microsoft.AspNetCore.Hosting
66
/// <summary>
77
/// Commonly used environment names.
88
/// </summary>
9+
[System.Obsolete("This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.Extensions.Hosting.Environments.", error: false)]
910
public static class EnvironmentName
1011
{
1112
public static readonly string Development = "Development";
1213
public static readonly string Staging = "Staging";
1314
public static readonly string Production = "Production";
1415
}
15-
}
16+
}

src/Hosting/Hosting/src/Internal/HostingEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
99
public class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment, IWebHostEnvironment
1010
#pragma warning restore CS0618 // Type or member is obsolete
1111
{
12-
public string EnvironmentName { get; set; } = Hosting.EnvironmentName.Production;
12+
public string EnvironmentName { get; set; } = Extensions.Hosting.Environments.Production;
1313

1414
public string ApplicationName { get; set; }
1515

src/Hosting/Hosting/test/StartupManagerTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Hosting.Tests.Internal;
1212
using Microsoft.Extensions.DependencyInjection;
1313
using Microsoft.Extensions.DependencyInjection.Extensions;
14+
using Microsoft.Extensions.Hosting;
1415
using Microsoft.Extensions.Options;
1516
using Xunit;
1617

@@ -510,7 +511,7 @@ public void CustomProviderFactoryCallsConfigureContainer()
510511
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
511512
var services = serviceCollection.BuildServiceProvider();
512513

513-
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
514+
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
514515

515516
var app = new ApplicationBuilder(services);
516517
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@@ -526,7 +527,7 @@ public void CustomServiceProviderFactoryStartupBaseClassCallsConfigureContainer(
526527
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
527528
var services = serviceCollection.BuildServiceProvider();
528529

529-
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development);
530+
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), Environments.Development);
530531

531532
var app = new ApplicationBuilder(services);
532533
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@@ -542,13 +543,13 @@ public void CustomServiceProviderFactoryEnvironmentBasedConfigureContainer()
542543
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
543544
var services = serviceCollection.BuildServiceProvider();
544545

545-
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), EnvironmentName.Production);
546+
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), Environments.Production);
546547

547548
var app = new ApplicationBuilder(services);
548549
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
549550

550551
Assert.IsType<MyContainer>(app.ApplicationServices);
551-
Assert.Equal(((MyContainer)app.ApplicationServices).Environment, EnvironmentName.Production);
552+
Assert.Equal(((MyContainer)app.ApplicationServices).Environment, Environments.Production);
552553
}
553554

554555
[Fact]
@@ -557,7 +558,7 @@ public void CustomServiceProviderFactoryThrowsIfNotRegisteredWithConfigureContai
557558
var serviceCollection = new ServiceCollection();
558559
var services = serviceCollection.BuildServiceProvider();
559560

560-
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
561+
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
561562

562563
Assert.Throws<InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection));
563564
}
@@ -568,7 +569,7 @@ public void CustomServiceProviderFactoryThrowsIfNotRegisteredWithConfigureContai
568569
var serviceCollection = new ServiceCollection();
569570
var services = serviceCollection.BuildServiceProvider();
570571

571-
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), EnvironmentName.Development));
572+
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), Environments.Development));
572573
}
573574

574575
[Fact]
@@ -578,7 +579,7 @@ public void CustomServiceProviderFactoryFailsWithOverloadsInStartup()
578579
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyContainerFactory>();
579580
var services = serviceCollection.BuildServiceProvider();
580581

581-
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupWithOverloads), EnvironmentName.Development));
582+
Assert.Throws<InvalidOperationException>(() => StartupLoader.LoadMethods(services, typeof(MyContainerStartupWithOverloads), Environments.Development));
582583
}
583584

584585
[Fact]
@@ -588,7 +589,7 @@ public void BadServiceProviderFactoryFailsThatReturnsNullServiceProviderOverridd
588589
serviceCollection.AddSingleton<IServiceProviderFactory<MyContainer>, MyBadContainerFactory>();
589590
var services = serviceCollection.BuildServiceProvider();
590591

591-
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), EnvironmentName.Development);
592+
var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development);
592593

593594
var app = new ApplicationBuilder(services);
594595
app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
@@ -629,12 +630,12 @@ public void ConfigureServices(IServiceCollection services)
629630

630631
public void ConfigureDevelopmentContainer(MyContainer container)
631632
{
632-
container.Environment = EnvironmentName.Development;
633+
container.Environment = Environments.Development;
633634
}
634635

635636
public void ConfigureProductionContainer(MyContainer container)
636637
{
637-
container.Environment = EnvironmentName.Production;
638+
container.Environment = Environments.Production;
638639
}
639640

640641
public void Configure(IApplicationBuilder app)

src/Hosting/Hosting/test/WebHostConfigurationsTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using Microsoft.AspNetCore.Hosting.Internal;
66
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
78
using Xunit;
89

910
namespace Microsoft.AspNetCore.Hosting.Tests
@@ -18,7 +19,7 @@ public void ReadsParametersCorrectly()
1819
{ WebHostDefaults.WebRootKey, "wwwroot"},
1920
{ WebHostDefaults.ApplicationKey, "MyProjectReference"},
2021
{ WebHostDefaults.StartupAssemblyKey, "MyProjectReference" },
21-
{ WebHostDefaults.EnvironmentKey, EnvironmentName.Development},
22+
{ WebHostDefaults.EnvironmentKey, Environments.Development},
2223
{ WebHostDefaults.DetailedErrorsKey, "true"},
2324
{ WebHostDefaults.CaptureStartupErrorsKey, "true" },
2425
{ WebHostDefaults.SuppressStatusMessagesKey, "true" }
@@ -29,7 +30,7 @@ public void ReadsParametersCorrectly()
2930
Assert.Equal("wwwroot", config.WebRoot);
3031
Assert.Equal("MyProjectReference", config.ApplicationName);
3132
Assert.Equal("MyProjectReference", config.StartupAssembly);
32-
Assert.Equal(EnvironmentName.Development, config.Environment);
33+
Assert.Equal(Environments.Development, config.Environment);
3334
Assert.True(config.CaptureStartupErrors);
3435
Assert.True(config.DetailedErrors);
3536
Assert.True(config.SuppressStatusMessages);
@@ -38,10 +39,10 @@ public void ReadsParametersCorrectly()
3839
[Fact]
3940
public void ReadsOldEnvKey()
4041
{
41-
var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", EnvironmentName.Development } };
42+
var parameters = new Dictionary<string, string>() { { "ENVIRONMENT", Environments.Development } };
4243
var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build());
4344

44-
Assert.Equal(EnvironmentName.Development, config.Environment);
45+
Assert.Equal(Environments.Development, config.Environment);
4546
}
4647

4748
[Theory]

src/Hosting/Hosting/test/WebHostTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,8 @@ public void EnvDefaultsToProductionIfNoConfig()
812812
#pragma warning disable CS0618 // Type or member is obsolete
813813
var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>();
814814
#pragma warning restore CS0618 // Type or member is obsolete
815-
Assert.Equal(EnvironmentName.Production, env.EnvironmentName);
816-
Assert.Equal(EnvironmentName.Production, env2.EnvironmentName);
815+
Assert.Equal(Environments.Production, env.EnvironmentName);
816+
Assert.Equal(Environments.Production, env2.EnvironmentName);
817817
}
818818
}
819819

@@ -822,7 +822,7 @@ public void EnvDefaultsToConfigValueIfSpecified()
822822
{
823823
var vals = new Dictionary<string, string>
824824
{
825-
{ "Environment", EnvironmentName.Staging }
825+
{ "Environment", Environments.Staging }
826826
};
827827

828828
var builder = new ConfigurationBuilder()
@@ -835,8 +835,8 @@ public void EnvDefaultsToConfigValueIfSpecified()
835835
#pragma warning disable CS0618 // Type or member is obsolete
836836
var env2 = host.Services.GetService<AspNetCore.Hosting.IHostingEnvironment>();
837837
#pragma warning restore CS0618 // Type or member is obsolete
838-
Assert.Equal(EnvironmentName.Staging, env.EnvironmentName);
839-
Assert.Equal(EnvironmentName.Staging, env.EnvironmentName);
838+
Assert.Equal(Environments.Staging, env.EnvironmentName);
839+
Assert.Equal(Environments.Staging, env.EnvironmentName);
840840
}
841841
}
842842

@@ -873,7 +873,7 @@ public async Task IsEnvironment_Extension_Is_Case_Insensitive()
873873
{
874874
await host.StartAsync();
875875
var env = host.Services.GetRequiredService<IHostEnvironment>();
876-
Assert.True(env.IsEnvironment(EnvironmentName.Production));
876+
Assert.True(env.IsEnvironment(Environments.Production));
877877
Assert.True(env.IsEnvironment("producTion"));
878878
}
879879
}

src/Hosting/samples/SampleStartups/StartupFullControl.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Hosting;
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
78
using Microsoft.Extensions.Logging;
89

910
// Note that this sample will not run. It is only here to illustrate usage patterns.
@@ -25,7 +26,7 @@ public static void Main(string[] args)
2526
.UseKestrel()
2627
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
2728
.UseUrls("http://*:1000", "https://*:902")
28-
.UseEnvironment(EnvironmentName.Development)
29+
.UseEnvironment(Environments.Development)
2930
.UseWebRoot("public")
3031
.ConfigureServices(services =>
3132
{

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Microsoft.Extensions.DependencyInjection;
1414
using Microsoft.Extensions.DependencyModel;
1515
using Microsoft.Extensions.Hosting;
16-
using EnvironmentName = Microsoft.Extensions.Hosting.EnvironmentName;
1716

1817
namespace Microsoft.AspNetCore.Mvc.Testing
1918
{
@@ -307,7 +306,7 @@ protected virtual IHostBuilder CreateHostBuilder()
307306
var hostBuilder = HostFactoryResolver.ResolveHostBuilderFactory<IHostBuilder>(typeof(TEntryPoint).Assembly)?.Invoke(Array.Empty<string>());
308307
if (hostBuilder != null)
309308
{
310-
hostBuilder.UseEnvironment(EnvironmentName.Development);
309+
hostBuilder.UseEnvironment(Environments.Development);
311310
}
312311
return hostBuilder;
313312
}
@@ -336,7 +335,7 @@ protected virtual IWebHostBuilder CreateWebHostBuilder()
336335
}
337336
else
338337
{
339-
return builder.UseEnvironment(EnvironmentName.Development);
338+
return builder.UseEnvironment(Environments.Development);
340339
}
341340
}
342341

0 commit comments

Comments
 (0)