.NET › Dependency Injection

How do third-party DI containers like Autofac integrate with ASP.NET Core's built-in DI abstractions?

Intermediate

Answer

ASP.NET Core's DI system is built around abstractions (IServiceCollection for registration, IServiceProvider for resolution) that third-party containers can replace entirely via a custom IServiceProviderFactory, letting you register services using the built-in IServiceCollection (for compatibility with framework and library registrations) while gaining access to advanced features the built-in container lacks, like multiple constructor resolution, property injection, or more sophisticated modules/scanning -- Autofac is the most common choice for teams needing these advanced capabilities.
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder => {
    containerBuilder.RegisterModule<MyAutofacModule>();
});
Real-world example A large enterprise application with complex conditional registration requirements (based on assembly scanning and convention-based module discovery) adopts Autofac specifically for its more powerful registration API, while still leveraging built-in framework services registered via standard IServiceCollection calls.

Common follow-ups: What features does Autofac provide that the built-in container lacks?;What's the performance trade-off of using a more feature-rich third-party container?