Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 5f14891

Browse files
PraburajHaoK
Praburaj
authored andcommitted
Add extension method for getting environment name
Fixes: #100
1 parent 89ae65e commit 5f14891

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Framework.Internal;
6+
7+
namespace Microsoft.AspNet.Hosting
8+
{
9+
public static class HostingEnvironmentExtensions
10+
{
11+
/// <summary>
12+
/// Compares the current hosting environment name against the specified value.
13+
/// </summary>
14+
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param>
15+
/// <param name="environmentName">Environment name to validate against.</param>
16+
/// <returns>True if the specified name is same as the current environment.</returns>
17+
public static bool IsEnvironment(
18+
[NotNull]this IHostingEnvironment hostingEnvironment,
19+
[NotNull]string environmentName)
20+
{
21+
return string.Equals(
22+
hostingEnvironment.EnvironmentName,
23+
environmentName,
24+
StringComparison.OrdinalIgnoreCase);
25+
}
26+
}
27+
}

src/Microsoft.AspNet.Hosting.Interfaces/project.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"Microsoft.AspNet.Http": "1.0.0-*",
55
"Microsoft.AspNet.FeatureModel": "1.0.0-*",
66
"Microsoft.AspNet.FileProviders.Interfaces": "1.0.0-*",
7-
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
7+
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
8+
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
89
},
910
"frameworks": {
1011
"dnx451": {},
1112
"dnxcore50": {}
1213
}
13-
}
14+
}

test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ public void WebRootCanBeResolvedFromTheProjectJson()
116116
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
117117
}
118118

119+
[Fact]
120+
public void Validate_Environment_Name()
121+
{
122+
var services = HostingServices.Create().BuildServiceProvider();
123+
var env = services.GetRequiredService<IHostingEnvironment>();
124+
Assert.Equal("Development", env.EnvironmentName);
125+
126+
var config = new Configuration()
127+
.AddCommandLine(new string[] { "--ASPNET_ENV", "Overridden_Environment" });
128+
129+
services = HostingServices.Create(fallbackServices: null, configuration: config)
130+
.BuildServiceProvider();
131+
132+
env = services.GetRequiredService<IHostingEnvironment>();
133+
Assert.Equal("Overridden_Environment", env.EnvironmentName);
134+
}
135+
136+
[Fact]
137+
public void IsEnvironment_Extension_Is_Case_Insensitive()
138+
{
139+
var services = HostingServices.Create().BuildServiceProvider();
140+
var env = services.GetRequiredService<IHostingEnvironment>();
141+
Assert.True(env.IsEnvironment("Development"));
142+
Assert.True(env.IsEnvironment("developMent"));
143+
}
144+
119145
public void Initialize(IApplicationBuilder builder)
120146
{
121147

0 commit comments

Comments
 (0)