From d852e10293c0fbd3dfbf82be455c2cde683ec0de Mon Sep 17 00:00:00 2001 From: Chris R Date: Wed, 28 Nov 2018 09:31:58 -0800 Subject: [PATCH] Hosting/#839 UseConfiguration with a config section --- ...ingAbstractionsWebHostBuilderExtensions.cs | 2 +- .../Hosting/test/WebHostBuilderTests.cs | 29 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs index f61b86eb86..1aac2e31d1 100644 --- a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs +++ b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Hosting /// The . 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); } diff --git a/src/Hosting/Hosting/test/WebHostBuilderTests.cs b/src/Hosting/Hosting/test/WebHostBuilderTests.cs index 876959ddb4..5a9f280340 100644 --- a/src/Hosting/Hosting/test/WebHostBuilderTests.cs +++ b/src/Hosting/Hosting/test/WebHostBuilderTests.cs @@ -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. using System; @@ -1129,6 +1129,33 @@ namespace Microsoft.AspNetCore.Hosting Assert.Contains("No application configured.", exception.Message); } + [Theory] + [MemberData(nameof(DefaultWebHostBuildersWithConfig))] + public void UseConfigurationWithSectionAddsSubKeys(IWebHostBuilder builder) + { + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new[] + { + new KeyValuePair("key", "value"), + new KeyValuePair("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(); + Assert.Equal("nestedvalue", appConfig["key"]); + } + } + private static void StaticConfigureMethod(IApplicationBuilder app) { } private IWebHostBuilder CreateWebHostBuilder()