Skip to content

Commit 91514c9

Browse files
authored
Update generic host wrapper config #6765 (#6770)
1 parent 17e5be6 commit 91514c9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ private WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext contex
327327
return webHostBuilderContext;
328328
}
329329

330-
return (WebHostBuilderContext)contextVal;
330+
// Refresh config, it's periodically updated/replaced
331+
var webHostContext = (WebHostBuilderContext)contextVal;
332+
webHostContext.Configuration = context.Configuration;
333+
return webHostContext;
331334
}
332335

333336
public string GetSetting(string key)

src/Hosting/Hosting/test/WebHostBuilderTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,54 @@ public void Build_ConfigureAppConfigurationInHostingStartupWorks(IWebHostBuilder
945945
}
946946
}
947947

948+
[Theory]
949+
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
950+
public void Build_AppConfigAvailableEverywhere(IWebHostBuilder builder)
951+
{
952+
builder = builder
953+
.CaptureStartupErrors(false)
954+
.ConfigureAppConfiguration((context, configurationBuilder) => {
955+
configurationBuilder.AddInMemoryCollection(
956+
new[]
957+
{
958+
new KeyValuePair<string,string>("appconfig", "appvalue")
959+
});
960+
})
961+
.ConfigureLogging((context, logging) =>
962+
{
963+
Assert.Equal("appvalue", context.Configuration["appconfig"]);
964+
})
965+
.ConfigureServices((context, services) =>
966+
{
967+
Assert.Equal("appvalue", context.Configuration["appconfig"]);
968+
})
969+
.UseDefaultServiceProvider((context, services) =>
970+
{
971+
Assert.Equal("appvalue", context.Configuration["appconfig"]);
972+
})
973+
.UseStartup<StartupCheckConfig>()
974+
.UseServer(new TestServer());
975+
976+
using (var host = builder.Build())
977+
{
978+
var configuration = host.Services.GetRequiredService<IConfiguration>();
979+
Assert.Equal("appvalue", configuration["appconfig"]);
980+
}
981+
}
982+
983+
public class StartupCheckConfig
984+
{
985+
public StartupCheckConfig(IConfiguration config)
986+
{
987+
Assert.Equal("value", config["testhostingstartup:config"]);
988+
}
989+
990+
public void Configure(IApplicationBuilder app)
991+
{
992+
993+
}
994+
}
995+
948996
[Theory]
949997
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
950998
public void Build_DoesRunHostingStartupFromPrimaryAssemblyEvenIfNotSpecified(IWebHostBuilder builder)

0 commit comments

Comments
 (0)