Dependency injection facilitates loose coupling and promotes testability and maintenance. ASP.Net Core provides (a kind of inversion of control) with a minimalistic dependency injection container. However, the built-in container lacks many of the features of a full-fledged dependency injection or inversion of control container.
To overcome this, you can use third-party containers in ASP.Net Core. In other words, you can easily replace the built-in container with a third-party container. Autofac is an inversion of control container that can be used to resolve dependencies. This article provides a discussion of how we can work with Autofac in ASP.Net Core.
Create an ASP.Net Core project in Visual Studio
First let’s create an ASP.Net Core project in Visual Studio. Assuming Visual Studio 2017 or Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core project in Visual Studio.
- Launch the Visual Studio IDE.
- Click on “Create new project.”
- In the “Create new project” window, select “ASP.Net Core Web Application” from the list of the templates displayed.
- Click Next.
- In the “Configure your new project” window, specify the name and location for the new project.
- Click Create.
- In the “Create New ASP.Net Core Web Application” window, select .Net Core as the runtime and ASP.Net Core 2.2 (or later) from the drop-down list at the top.
- Select “Web Application” as the project template.
- Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
- Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either.
- Click Create.
This will create a new ASP.Net Core project in Visual Studio. We’ll use this project in the subsequent sections of this article to work with Autofac.