Use Autofac IoC Container in ASP.NET or ASP.NET Core

Ashish Patel
.NET Hub
Published in
2 min readApr 22, 2021

--

Getting started — How to use Autofac for dependency injection in .NET and .NET Core?

Autofac in .NET

TL;DR

Autofac is an IoC container for .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. Autofac is the most popular DI/IoC container for ASP.NET and it works with .NET Core flawlessly.

.NET Core gives you a built-in dependency injection framework. Though the default DI may offer enough functionality, there is a certain limitations like resolving a service with some associated Metadata, Named/Keyed services, Aggregate Services, Multi-tenant support, lazy instantiation, and much more. As the system grows you might need such features, and Autofac gives you all these features.

Most popular .NET Libraries every developer should know.

Configure Autofac in ASP.NET Core application

1. NuGet: To use Autofac, you need to install below NuGet packages.

PM> Install-Package Autofac
PM> Install-Package Autofac.Extensions.DependencyInjection

2. Configuration: Configure Host builder, typically this is your Program.cs and insert .UseServiceProviderFactory(new AutofacServiceProviderFactory()). The core…

--

--

Ashish Patel
.NET Hub

Cloud Architect • 4x AWS Certified • 6x Azure Certified • 1x Kubernetes Certified • MCP • .NET • Terraform • DevOps • Blogger [https://bit.ly/iamashishpatel]