Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/libraries/Microsoft.Extensions.Hosting/src/Host.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -57,9 +58,12 @@ public static IHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new HostBuilder();

builder.UseContentRoot(Directory.GetCurrentDirectory());
builder.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string> (HostDefaults.ContentRootKey, Directory.GetCurrentDirectory())
});
config.AddEnvironmentVariables(prefix: "DOTNET_");
if (args != null)
{
Expand Down
19 changes: 17 additions & 2 deletions src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,26 @@ private void BuildHostConfiguration()

private void CreateHostingEnvironment()
{
// Explicit settings override config.
string environmentName = _hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production;
if (Properties.TryGetValue(HostDefaults.EnvironmentKey, out object envObj)
&& (envObj is string env))
{
environmentName = env;
}

string contentRoot = _hostConfiguration[HostDefaults.ContentRootKey];
if (Properties.TryGetValue(HostDefaults.ContentRootKey, out object contentObj)
&& (contentObj is string content))
{
contentRoot = content;
}

_hostingEnvironment = new HostingEnvironment()
{
ApplicationName = _hostConfiguration[HostDefaults.ApplicationKey],
EnvironmentName = _hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production,
ContentRootPath = ResolveContentRootPath(_hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory),
EnvironmentName = environmentName,
ContentRootPath = ResolveContentRootPath(contentRoot, AppContext.BaseDirectory),
};

if (string.IsNullOrEmpty(_hostingEnvironment.ApplicationName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
Expand All @@ -22,14 +21,8 @@ public static class HostingHostBuilderExtensions
/// <returns>The <see cref="IHostBuilder"/>.</returns>
public static IHostBuilder UseEnvironment(this IHostBuilder hostBuilder, string environment)
{
return hostBuilder.ConfigureHostConfiguration(configBuilder =>
{
configBuilder.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(HostDefaults.EnvironmentKey,
environment ?? throw new ArgumentNullException(nameof(environment)))
});
});
hostBuilder.Properties[HostDefaults.EnvironmentKey] = environment;
return hostBuilder;
}

/// <summary>
Expand All @@ -40,14 +33,8 @@ public static IHostBuilder UseEnvironment(this IHostBuilder hostBuilder, string
/// <returns>The <see cref="IHostBuilder"/>.</returns>
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder, string contentRoot)
{
return hostBuilder.ConfigureHostConfiguration(configBuilder =>
{
configBuilder.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(HostDefaults.ContentRootKey,
contentRoot ?? throw new ArgumentNullException(nameof(contentRoot)))
});
});
hostBuilder.Properties[HostDefaults.ContentRootKey] = contentRoot;
return hostBuilder;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,38 @@ public void ConfigBasedSettingsConfigBasedOverride()
}

[Fact]
public void UseEnvironmentIsNotOverriden()
public void UseEnvironmentOverridesConfig()
{
var vals = new Dictionary<string, string>
{
{ "ENV", "Dev" },
{ HostDefaults.EnvironmentKey, "Dev" },
};

var expected = "MY_TEST_ENVIRONMENT";

using (var host = new HostBuilder()
.ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals))
.UseEnvironment(expected)
.Build())
{
Assert.Equal(expected, host.Services.GetService<IHostEnvironment>().EnvironmentName);
}
}

[Fact]
public void UseEnvironmentIsNotOverridenByConfig()
{
var vals = new Dictionary<string, string>
{
{ HostDefaults.EnvironmentKey, "Dev" },
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(vals);
var config = builder.Build();

var expected = "MY_TEST_ENVIRONMENT";


using (var host = new HostBuilder()
.ConfigureHostConfiguration(configBuilder => configBuilder.AddConfiguration(config))
.UseEnvironment(expected)
.ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals))
.Build())
{
Assert.Equal(expected, host.Services.GetService<IHostEnvironment>().EnvironmentName);
Expand All @@ -232,19 +248,44 @@ public void BuildAndDispose()
}

[Fact]
public void UseBasePathConfiguresBasePath()
public void UseContentRootConfiguresContentRoot()
{
using (var host = new HostBuilder()
.UseContentRoot("/")
.Build())
{
Assert.Equal("/", host.Services.GetService<IHostEnvironment>().ContentRootPath);
}
}

[Fact]
public void UseContentRootOverridesConfig()
{
var vals = new Dictionary<string, string>
{
{ "ENV", "Dev" },
{ HostDefaults.ContentRootKey, "Foo" },
};

using (var host = new HostBuilder()
.ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals))
.UseContentRoot("/")
.Build())
{
Assert.Equal("/", host.Services.GetService<IHostEnvironment>().ContentRootPath);
}
}

[Fact]
public void UseContentRootNotOverridenByConfig()
{
var vals = new Dictionary<string, string>
{
{ HostDefaults.ContentRootKey, "Foo" },
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(vals);
var config = builder.Build();

using (var host = new HostBuilder()
.ConfigureHostConfiguration(configBuilder => configBuilder.AddConfiguration(config))
.UseContentRoot("/")
.ConfigureHostConfiguration(configBuilder => configBuilder.AddInMemoryCollection(vals))
.Build())
{
Assert.Equal("/", host.Services.GetService<IHostEnvironment>().ContentRootPath);
Expand Down