.NET Core 3.X中IStartup接口ConfigureServices方法签名变更适配疑问及替代命名空间咨询
Great question! Let's break down the reasoning behind this change and the alternatives available to you.
Microsoft.AspNetCore.Hosting.IStartup Didn't Adapt to the Void Return Signature First, it's important to understand the shift in .NET Core's hosting model introduced in 3.x. Prior to 3.x, the Web Host (tied to Microsoft.AspNetCore.Hosting) was the primary way to run web apps, and the IStartup interface was designed around allowing developers to replace the entire service provider via the IServiceProvider return type of ConfigureServices.
When .NET Core 3.x launched, the Generic Host (under Microsoft.Extensions.Hosting) became the default hosting model for all app types (web, console, etc.). This new model simplified service configuration workflows, and the decision to make ConfigureServices return void was part of that simplification:
- The void return type aligns with the Generic Host's approach of building the service provider centrally rather than letting individual Startup classes override it directly.
- If you still need to customize the service provider (e.g., using a DI container like Autofac), the Generic Host provides dedicated mechanisms (like
UseServiceProviderFactory) that are more flexible than returning anIServiceProviderfromConfigureServices.
Microsoft chose not to update the IStartup interface in Microsoft.AspNetCore.Hosting because it's tied to the legacy Web Host model. The team focused on investing in the Generic Host instead, while keeping IStartup intact for backward compatibility with existing 2.x projects upgrading to 3.x+.
If you're working with .NET Core 3.x+ and want to align with the modern hosting model, here's what you should use:
Microsoft.Extensions.Hosting: This is the core namespace for the Generic Host, which is now the default for all .NET Core/5+ web projects. You don't need to implementIStartupat all—yourStartupclass just needs to have thepublic void ConfigureServices(IServiceCollection services)andpublic void Configure(IApplicationBuilder app)methods. The host builder (viaHost.CreateDefaultBuilder(args).ConfigureWebHostDefaults(...)) will automatically discover and use these methods.- Custom Service Provider Configuration: If you need to replace the default service provider, use
IServiceProviderFactory<TContainerBuilder>along withHostBuilder.UseServiceProviderFactory(). For example, integrating Autofac would look like this:Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); IStartupFilter: If you need to modify the middleware pipeline around your Startup'sConfiguremethod (similar to extendingIStartup), use this interface fromMicrosoft.AspNetCore.Hosting—it’s still relevant in the modern model for adding pre/post middleware logic.
Remember, the Microsoft.AspNetCore.Hosting.IStartup interface is essentially a legacy component now. For new projects or when upgrading, sticking to the Generic Host model under Microsoft.Extensions.Hosting is the recommended approach.
内容的提问来源于stack exchange,提问作者Sameed

