diff --git a/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs b/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs new file mode 100644 index 00000000..b39316a2 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Open Technologies, Inc. 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.Framework.Internal; + +namespace Microsoft.AspNet.Hosting +{ + public static class HostingEnvironmentExtensions + { + /// + /// Compares the current hosting environment name against the specified value. + /// + /// An instance of service. + /// Environment name to validate against. + /// True if the specified name is same as the current environment. + public static bool IsEnvironment( + [NotNull]this IHostingEnvironment hostingEnvironment, + [NotNull]string environmentName) + { + return string.Equals( + hostingEnvironment.EnvironmentName, + environmentName, + StringComparison.OrdinalIgnoreCase); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting.Interfaces/project.json b/src/Microsoft.AspNet.Hosting.Interfaces/project.json index 496ff144..e21eb01c 100644 --- a/src/Microsoft.AspNet.Hosting.Interfaces/project.json +++ b/src/Microsoft.AspNet.Hosting.Interfaces/project.json @@ -4,10 +4,11 @@ "Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.FeatureModel": "1.0.0-*", "Microsoft.AspNet.FileProviders.Interfaces": "1.0.0-*", - "Microsoft.Framework.ConfigurationModel": "1.0.0-*" + "Microsoft.Framework.ConfigurationModel": "1.0.0-*", + "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" } }, "frameworks": { "dnx451": {}, "dnxcore50": {} } -} +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index d66e77a3..d6e1e64b 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -63,6 +63,32 @@ public void WebRootCanBeResolvedFromTheProjectJson() Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists); } + [Fact] + public void Validate_Environment_Name() + { + var services = HostingServices.Create().BuildServiceProvider(); + var env = services.GetRequiredService(); + Assert.Equal("Development", env.EnvironmentName); + + var config = new Configuration() + .AddCommandLine(new string[] { "--ASPNET_ENV", "Overridden_Environment" }); + + services = HostingServices.Create(fallbackServices: null, configuration: config) + .BuildServiceProvider(); + + env = services.GetRequiredService(); + Assert.Equal("Overridden_Environment", env.EnvironmentName); + } + + [Fact] + public void IsEnvironment_Extension_Is_Case_Insensitive() + { + var services = HostingServices.Create().BuildServiceProvider(); + var env = services.GetRequiredService(); + Assert.True(env.IsEnvironment("Development")); + Assert.True(env.IsEnvironment("developMent")); + } + public void Initialize(IApplicationBuilder builder) {