Flow configuration via IHostingEnvironment

This commit is contained in:
Pavel Krymets 2015-11-19 10:43:08 -08:00
parent 7c8b37bcc7
commit 04c30c8bb7
6 changed files with 33 additions and 4 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.FileProviders;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNet.Hosting
{
@ -28,5 +29,10 @@ namespace Microsoft.AspNet.Hosting
/// </summary>
// This must be settable!
IFileProvider WebRootFileProvider { get; set; }
/// <summary>
/// Gets or sets the configuration object used by hosting environment.
/// </summary>
IConfiguration Configuration { get; set; }
}
}

View File

@ -11,7 +11,8 @@
},
"dependencies": {
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*"
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-*"
},
"frameworks": {
"net451": {},

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting.Internal;
using Microsoft.Extensions.Configuration;
namespace Microsoft.AspNet.Hosting
{
@ -13,5 +13,7 @@ namespace Microsoft.AspNet.Hosting
public string WebRootPath { get; set; }
public IFileProvider WebRootFileProvider { get; set; }
public IConfiguration Configuration { get; set; }
}
}

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Hosting
{
public static class HostingEnvironmentExtensions
{
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, WebHostOptions options)
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, WebHostOptions options, IConfiguration configuration)
{
if (options == null)
{
@ -46,6 +46,8 @@ namespace Microsoft.AspNet.Hosting
var environmentName = options.Environment;
hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName;
hostingEnvironment.Configuration = configuration;
}
}
}

View File

@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Hosting
var appEnvironment = hostingContainer.GetRequiredService<IApplicationEnvironment>();
var startupLoader = hostingContainer.GetRequiredService<IStartupLoader>();
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _options);
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _options, _config);
if (!string.IsNullOrEmpty(_environmentName))
{
_hostingEnvironment.EnvironmentName = _environmentName;

View File

@ -144,6 +144,24 @@ namespace Microsoft.AspNet.Hosting
Assert.Equal("http://localhost:5000", app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.First());
}
[Fact]
public void FlowsConfig()
{
var vals = new Dictionary<string, string>
{
{ "Server", "Microsoft.AspNet.Hosting.Tests" }
};
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(vals);
var config = builder.Build();
var host = CreateBuilder(config).Build();
var app = host.Start();
var hostingEnvironment = host.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.NotNull(hostingEnvironment.Configuration);
Assert.Equal("Microsoft.AspNet.Hosting.Tests", hostingEnvironment.Configuration["Server"]);
}
[Fact]
public void HostingEngineCanBeStarted()
{