From a16a6dfaeb52cda2b853ba46c75fc96d56f24e00 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 25 Jan 2019 08:16:39 -0800 Subject: [PATCH] Block generic host Startup.ConfigureServices that return IServiceProvider #5149 (#6997) --- .../src/GenericHost/GenericWebHostBuilder.cs | 4 ++++ .../Hosting/src/Internal/StartupLoader.cs | 7 ++++++- ...s.cs => StartupWithBuiltConfigureServices.cs} | 4 ++-- src/Hosting/Hosting/test/WebHostBuilderTests.cs | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) rename src/Hosting/Hosting/test/Fakes/{StartupWithNullConfigureServices.cs => StartupWithBuiltConfigureServices.cs} (86%) diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index 80c8c76ef4..5f04798c41 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -232,6 +232,10 @@ namespace Microsoft.AspNetCore.Hosting.Internal { throw new NotSupportedException($"{typeof(IStartup)} isn't supported"); } + if (StartupLoader.HasConfigureServicesIServiceProviderDelegate(startupType, context.HostingEnvironment.EnvironmentName)) + { + throw new NotSupportedException($"ConfigureServices returning an {typeof(IServiceProvider)} isn't supported."); + } instance = ActivatorUtilities.CreateInstance(new HostServiceProvider(webHostBuilderContext), startupType); context.Properties[_startupKey] = instance; diff --git a/src/Hosting/Hosting/src/Internal/StartupLoader.cs b/src/Hosting/Hosting/src/Internal/StartupLoader.cs index 72dab81736..e4121da4fb 100644 --- a/src/Hosting/Hosting/src/Internal/StartupLoader.cs +++ b/src/Hosting/Hosting/src/Internal/StartupLoader.cs @@ -279,6 +279,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal return new ConfigureContainerBuilder(configureMethod); } + internal static bool HasConfigureServicesIServiceProviderDelegate(Type startupType, string environmentName) + { + return null != FindMethod(startupType, "Configure{0}Services", environmentName, typeof(IServiceProvider), required: false); + } + internal static ConfigureServicesBuilder FindConfigureServicesDelegate(Type startupType, string environmentName) { var servicesMethod = FindMethod(startupType, "Configure{0}Services", environmentName, typeof(IServiceProvider), required: false) @@ -333,4 +338,4 @@ namespace Microsoft.AspNetCore.Hosting.Internal return methodInfo; } } -} \ No newline at end of file +} diff --git a/src/Hosting/Hosting/test/Fakes/StartupWithNullConfigureServices.cs b/src/Hosting/Hosting/test/Fakes/StartupWithBuiltConfigureServices.cs similarity index 86% rename from src/Hosting/Hosting/test/Fakes/StartupWithNullConfigureServices.cs rename to src/Hosting/Hosting/test/Fakes/StartupWithBuiltConfigureServices.cs index 9390f4727f..9a61d46ef4 100644 --- a/src/Hosting/Hosting/test/Fakes/StartupWithNullConfigureServices.cs +++ b/src/Hosting/Hosting/test/Fakes/StartupWithBuiltConfigureServices.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Hosting.Fakes { - public class StartupWithNullConfigureServices + public class StartupWithBuiltConfigureServices { public IServiceProvider ConfigureServices(IServiceCollection services) { @@ -13,4 +13,4 @@ namespace Microsoft.AspNetCore.Hosting.Fakes public void Configure(IApplicationBuilder app) { } } -} \ No newline at end of file +} diff --git a/src/Hosting/Hosting/test/WebHostBuilderTests.cs b/src/Hosting/Hosting/test/WebHostBuilderTests.cs index fd87f235fd..670a57b68d 100644 --- a/src/Hosting/Hosting/test/WebHostBuilderTests.cs +++ b/src/Hosting/Hosting/test/WebHostBuilderTests.cs @@ -889,6 +889,22 @@ namespace Microsoft.AspNetCore.Hosting Assert.Equal("Building this implementation of IWebHostBuilder is not supported.", exception.Message); } + [Fact] + public void GenericWebHostDoesNotSupportBuildingInConfigureServices() + { + var hostBuilder = new HostBuilder() + .ConfigureWebHost(builder => + { + builder.UseStartup(); + }); + var exception = Assert.Throws(() => + { + hostBuilder.Build(); + }); + + Assert.Equal($"ConfigureServices returning an {typeof(IServiceProvider)} isn't supported.", exception.Message); + } + [Theory] [MemberData(nameof(DefaultWebHostBuildersWithConfig))] public void Build_HostingStartupAssemblyCanBeExcluded(IWebHostBuilder builder)