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)