Use Autofac IoC Container in ASP.NET or ASP.NET Core
Getting started — How to use Autofac for dependency injection in .NET and .NET Core?
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.
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…