Hosting/#839 UseConfiguration with a config section
This commit is contained in:
parent
c0381de983
commit
d852e10293
|
|
@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
|
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
|
||||||
public static IWebHostBuilder UseConfiguration(this IWebHostBuilder hostBuilder, IConfiguration configuration)
|
public static IWebHostBuilder UseConfiguration(this IWebHostBuilder hostBuilder, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
foreach (var setting in configuration.AsEnumerable())
|
foreach (var setting in configuration.AsEnumerable(makePathsRelative: true))
|
||||||
{
|
{
|
||||||
hostBuilder.UseSetting(setting.Key, setting.Value);
|
hostBuilder.UseSetting(setting.Key, setting.Value);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -1129,6 +1129,33 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
Assert.Contains("No application configured.", exception.Message);
|
Assert.Contains("No application configured.", exception.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
|
||||||
|
public void UseConfigurationWithSectionAddsSubKeys(IWebHostBuilder builder)
|
||||||
|
{
|
||||||
|
var config = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(new[]
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("key", "value"),
|
||||||
|
new KeyValuePair<string, string>("nested:key", "nestedvalue"),
|
||||||
|
}).Build();
|
||||||
|
var section = config.GetSection("nested");
|
||||||
|
|
||||||
|
builder = builder
|
||||||
|
.CaptureStartupErrors(false)
|
||||||
|
.Configure(app => { })
|
||||||
|
.UseConfiguration(section)
|
||||||
|
.UseServer(new TestServer());
|
||||||
|
|
||||||
|
Assert.Equal("nestedvalue", builder.GetSetting("key"));
|
||||||
|
|
||||||
|
using (var host = builder.Build())
|
||||||
|
{
|
||||||
|
var appConfig = host.Services.GetRequiredService<IConfiguration>();
|
||||||
|
Assert.Equal("nestedvalue", appConfig["key"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void StaticConfigureMethod(IApplicationBuilder app) { }
|
private static void StaticConfigureMethod(IApplicationBuilder app) { }
|
||||||
|
|
||||||
private IWebHostBuilder CreateWebHostBuilder()
|
private IWebHostBuilder CreateWebHostBuilder()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue