Skip to content

Commit a16a6df

Browse files
authored
Block generic host Startup.ConfigureServices that return IServiceProvider #5149 (#6997)
1 parent aef117f commit a16a6df

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ private void UseStartup(Type startupType, HostBuilderContext context, IServiceCo
232232
{
233233
throw new NotSupportedException($"{typeof(IStartup)} isn't supported");
234234
}
235+
if (StartupLoader.HasConfigureServicesIServiceProviderDelegate(startupType, context.HostingEnvironment.EnvironmentName))
236+
{
237+
throw new NotSupportedException($"ConfigureServices returning an {typeof(IServiceProvider)} isn't supported.");
238+
}
235239

236240
instance = ActivatorUtilities.CreateInstance(new HostServiceProvider(webHostBuilderContext), startupType);
237241
context.Properties[_startupKey] = instance;

src/Hosting/Hosting/src/Internal/StartupLoader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ internal static ConfigureContainerBuilder FindConfigureContainerDelegate(Type st
279279
return new ConfigureContainerBuilder(configureMethod);
280280
}
281281

282+
internal static bool HasConfigureServicesIServiceProviderDelegate(Type startupType, string environmentName)
283+
{
284+
return null != FindMethod(startupType, "Configure{0}Services", environmentName, typeof(IServiceProvider), required: false);
285+
}
286+
282287
internal static ConfigureServicesBuilder FindConfigureServicesDelegate(Type startupType, string environmentName)
283288
{
284289
var servicesMethod = FindMethod(startupType, "Configure{0}Services", environmentName, typeof(IServiceProvider), required: false)
@@ -333,4 +338,4 @@ private static MethodInfo FindMethod(Type startupType, string methodName, string
333338
return methodInfo;
334339
}
335340
}
336-
}
341+
}

src/Hosting/Hosting/test/Fakes/StartupWithNullConfigureServices.cs renamed to src/Hosting/Hosting/test/Fakes/StartupWithBuiltConfigureServices.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Microsoft.AspNetCore.Hosting.Fakes
66
{
7-
public class StartupWithNullConfigureServices
7+
public class StartupWithBuiltConfigureServices
88
{
99
public IServiceProvider ConfigureServices(IServiceCollection services)
1010
{
@@ -13,4 +13,4 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
1313

1414
public void Configure(IApplicationBuilder app) { }
1515
}
16-
}
16+
}

src/Hosting/Hosting/test/WebHostBuilderTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,22 @@ public void GenericWebHostThrowsOnBuild()
889889
Assert.Equal("Building this implementation of IWebHostBuilder is not supported.", exception.Message);
890890
}
891891

892+
[Fact]
893+
public void GenericWebHostDoesNotSupportBuildingInConfigureServices()
894+
{
895+
var hostBuilder = new HostBuilder()
896+
.ConfigureWebHost(builder =>
897+
{
898+
builder.UseStartup<StartupWithBuiltConfigureServices>();
899+
});
900+
var exception = Assert.Throws<NotSupportedException>(() =>
901+
{
902+
hostBuilder.Build();
903+
});
904+
905+
Assert.Equal($"ConfigureServices returning an {typeof(IServiceProvider)} isn't supported.", exception.Message);
906+
}
907+
892908
[Theory]
893909
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
894910
public void Build_HostingStartupAssemblyCanBeExcluded(IWebHostBuilder builder)

0 commit comments

Comments
 (0)