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

Commit 3b0d5fd

Browse files
author
Praburaj
committed
Add extension method for getting environment name
Fixes: #100
1 parent a10acfd commit 3b0d5fd

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
@@ -63,6 +63,32 @@ public void WebRootCanBeResolvedFromTheProjectJson()
6363
Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists);
6464
}
6565

66+
[Fact]
67+
public void Validate_Environment_Name()
68+
{
69+
var services = HostingServices.Create().BuildServiceProvider();
70+
var env = services.GetRequiredService<IHostingEnvironment>();
71+
Assert.Equal("Development", env.EnvironmentName);
72+
73+
var config = new Configuration()
74+
.AddCommandLine(new string[] { "--ASPNET_ENV", "Overridden_Environment" });
75+
76+
services = HostingServices.Create(fallbackServices: null, configuration: config)
77+
.BuildServiceProvider();
78+
79+
env = services.GetRequiredService<IHostingEnvironment>();
80+
Assert.Equal("Overridden_Environment", env.EnvironmentName);
81+
}
82+
83+
[Fact]
84+
public void IsEnvironment_Extension_Is_Case_Insensitive()
85+
{
86+
var services = HostingServices.Create().BuildServiceProvider();
87+
var env = services.GetRequiredService<IHostingEnvironment>();
88+
Assert.True(env.IsEnvironment("Development"));
89+
Assert.True(env.IsEnvironment("developMent"));
90+
}
91+
6692
public void Initialize(IApplicationBuilder builder)
6793
{
6894

0 commit comments

Comments
 (0)