Add extension method for getting environment name

Fixes: https://github.com/aspnet/Hosting/issues/100
This commit is contained in:
Praburaj 2015-03-16 13:13:50 -07:00
parent a10acfd4cd
commit 3b0d5fd422
3 changed files with 56 additions and 2 deletions

View File

@ -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
{
/// <summary>
/// Compares the current hosting environment name against the specified value.
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param>
/// <param name="environmentName">Environment name to validate against.</param>
/// <returns>True if the specified name is same as the current environment.</returns>
public static bool IsEnvironment(
[NotNull]this IHostingEnvironment hostingEnvironment,
[NotNull]string environmentName)
{
return string.Equals(
hostingEnvironment.EnvironmentName,
environmentName,
StringComparison.OrdinalIgnoreCase);
}
}
}

View File

@ -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": {}
}
}
}

View File

@ -63,6 +63,32 @@ namespace Microsoft.AspNet.Hosting
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
}
[Fact]
public void Validate_Environment_Name()
{
var services = HostingServices.Create().BuildServiceProvider();
var env = services.GetRequiredService<IHostingEnvironment>();
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<IHostingEnvironment>();
Assert.Equal("Overridden_Environment", env.EnvironmentName);
}
[Fact]
public void IsEnvironment_Extension_Is_Case_Insensitive()
{
var services = HostingServices.Create().BuildServiceProvider();
var env = services.GetRequiredService<IHostingEnvironment>();
Assert.True(env.IsEnvironment("Development"));
Assert.True(env.IsEnvironment("developMent"));
}
public void Initialize(IApplicationBuilder builder)
{