From d24ec01224726fa85dc7683d0f70a94d9172bc94 Mon Sep 17 00:00:00 2001 From: Daniel Marbach Date: Thu, 28 Mar 2019 18:10:06 +0100 Subject: [PATCH] Add overload for Configure that gets WebHostBuilderContext (#8697) --- ...rosoft.AspNetCore.Hosting.netcoreapp3.0.cs | 1 + .../src/GenericHost/GenericWebHostBuilder.cs | 5 +++-- .../HostingStartupWebHostBuilder.cs | 2 +- .../src/GenericHost/ISupportsStartup.cs | 2 +- .../Hosting/src/WebHostBuilderExtensions.cs | 22 +++++++++++++++---- .../Fakes/GenericWebHostBuilderWrapper.cs | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Hosting/Hosting/ref/Microsoft.AspNetCore.Hosting.netcoreapp3.0.cs b/src/Hosting/Hosting/ref/Microsoft.AspNetCore.Hosting.netcoreapp3.0.cs index da2e337570..1b7b978add 100644 --- a/src/Hosting/Hosting/ref/Microsoft.AspNetCore.Hosting.netcoreapp3.0.cs +++ b/src/Hosting/Hosting/ref/Microsoft.AspNetCore.Hosting.netcoreapp3.0.cs @@ -35,6 +35,7 @@ namespace Microsoft.AspNetCore.Hosting public static partial class WebHostBuilderExtensions { public static Microsoft.AspNetCore.Hosting.IWebHostBuilder Configure(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder, System.Action configureApp) { throw null; } + public static Microsoft.AspNetCore.Hosting.IWebHostBuilder Configure(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder, System.Action configureApp) { throw null; } public static Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureAppConfiguration(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder, System.Action configureDelegate) { throw null; } public static Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureLogging(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder, System.Action configureLogging) { throw null; } public static Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureLogging(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder, System.Action configureLogging) { throw null; } diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index d463c5b36d..894214137c 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -303,13 +303,14 @@ namespace Microsoft.AspNetCore.Hosting.Internal builder.Build(instance)(container); } - public IWebHostBuilder Configure(Action configure) + public IWebHostBuilder Configure(Action configure) { _builder.ConfigureServices((context, services) => { services.Configure(options => { - options.ConfigureApplication = configure; + var webhostBuilderContext = GetWebHostBuilderContext(context); + options.ConfigureApplication = app => configure(webhostBuilderContext, app); }); }); diff --git a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs index 52eaec8179..65c096e965 100644 --- a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal return _builder.UseDefaultServiceProvider(configure); } - public IWebHostBuilder Configure(Action configure) + public IWebHostBuilder Configure(Action configure) { return _builder.Configure(configure); } diff --git a/src/Hosting/Hosting/src/GenericHost/ISupportsStartup.cs b/src/Hosting/Hosting/src/GenericHost/ISupportsStartup.cs index 16322c8bea..0f3be6b209 100644 --- a/src/Hosting/Hosting/src/GenericHost/ISupportsStartup.cs +++ b/src/Hosting/Hosting/src/GenericHost/ISupportsStartup.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal { internal interface ISupportsStartup { - IWebHostBuilder Configure(Action configure); + IWebHostBuilder Configure(Action configure); IWebHostBuilder UseStartup(Type startupType); } } diff --git a/src/Hosting/Hosting/src/WebHostBuilderExtensions.cs b/src/Hosting/Hosting/src/WebHostBuilderExtensions.cs index 60acc075fa..f6ea1c797e 100644 --- a/src/Hosting/Hosting/src/WebHostBuilderExtensions.cs +++ b/src/Hosting/Hosting/src/WebHostBuilderExtensions.cs @@ -22,14 +22,28 @@ namespace Microsoft.AspNetCore.Hosting /// The delegate that configures the . /// The . public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action configureApp) + { + return hostBuilder.Configure((_, app) => configureApp(app), configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name); + } + + /// + /// Specify the startup method to be used to configure the web application. + /// + /// The to configure. + /// The delegate that configures the . + /// The . + public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action configureApp) + { + return hostBuilder.Configure(configureApp, configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name); + } + + private static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action configureApp, string startupAssemblyName) { if (configureApp == null) { throw new ArgumentNullException(nameof(configureApp)); } - var startupAssemblyName = configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name; - hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName); // Light up the ISupportsStartup implementation @@ -38,11 +52,11 @@ namespace Microsoft.AspNetCore.Hosting return supportsStartup.Configure(configureApp); } - return hostBuilder.ConfigureServices(services => + return hostBuilder.ConfigureServices((context, services) => { services.AddSingleton(sp => { - return new DelegateStartup(sp.GetRequiredService>(), configureApp); + return new DelegateStartup(sp.GetRequiredService>(), (app => configureApp(context, app))); }); }); } diff --git a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs index 3ff3aeef51..eaa484a820 100644 --- a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs +++ b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests.Fakes return new GenericWebHost(_hostBuilder.Build()); } - public IWebHostBuilder Configure(Action configure) + public IWebHostBuilder Configure(Action configure) { _builder.Configure(configure); return this;