How to use Autofac in ASP.Net Core

Dependency injection facilitates loose coupling and promotes testability and maintenance. ASP.Net Core provides built-in support for dependency injection (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.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.Net Core Web Application” from the list of the templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Click Create.
  7. 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.
  8. Select “Web Application” as the project template. 
  9. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
  10. Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either.
  11. 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.

You may also like...

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

       
Apps & Games Clothing Electronics & Photo Large Appliances
Baby Womens Apparel Garden Lighting
Beauty Mens Apparel Outdoors Luggage
Books Girls Apparel Health & Personal Care Pet Supplies
Car Boys Apparel Home Shoes & Bags
Motorbike Computers & Accessories Kitchen Equipment Sports & Outdoors
Fashion DIY & Tools Jewellery Toys & Games