You are currently viewing How To Use Autofac Container with .NET Web Applications

How To Use Autofac Container with .NET Web Applications

We know that .NET has its own built-in IoC container. When you create web applications, the project templates include the defautl IoC container so that you can focus on your application logic.

But if you can always go ahead and modify the application’s bootstrapping logic to use Autofac instead of the .NET‘s default container. In this article, let’s have a look at how this can be achieved.

Create Project

For this demo, let’s create a .NET core MVC web application. You can refer first few steps of this article to create the .NET Core MVC application using Visual Studio.

Although are are using MVC for this demo, you can use these configurations for Web APIs or any other type of projects, as long as you are using same interfaces to bootstrap the application.

Autofac Extension Package

Now, we will have to add the reference to the Autofac.Extensions.DependencyInjection package from NuGet.

Attach to Host

In this section, let’s attach the AutofacServiceProviderFactory to the IHostBuilder. Open Program.cs file from the project and let’s modify the main method to add the provider factory as shown in below code snippet.

Startup Modifications

Startup class mainly contains two methods:

  • Configure method, which defines the request processing pipeline for the .NET Core web app
  • ConfigureServices method, which is for configuring the services which would be used by the application.

Now, you can add third method ConfigureContainer method to bootstrap the Autofac container. You can also create environment specific ConfigureContainer method by inserting environment name in between as explained in this post.

Services

Let’s create 4 interfaces as shown in below code snippet. First interface contains the method that we will use to return the object creation time.

Then we also have three helper interfaces to demonstrate when (time) objects are created and disposed in the container. These three interfaces are just marker interfaces to demonstrate the service lifetimes.

Then let’s implement a class which implements all the three special interfaces as shown below.

Create Root Module

Create a new class AutofacRootModule. In the root module now, and register the interfaces to the container as shown below.

Home Controller

Here, we need to modify two things, the controller action and the corresponding view. For the sake of simplicity, let’s modify the Index action and its view.

Inject the interfaces in the controller.

Then read the property CreatedOn and populate it in the ViewData collection in the Index action.

Next, show those values on Index view. The sample HTML is also shown in below code snippet.

Run and Verify

Now our application should compile successfully and should be ready to run.

If we run the application now, we should be able to see the created times as shown in below snapshot.

If the page is refreshed, the singleton creation time would stay same always. And every time the page is refreshed, the creation time for scoped and transient object would change.

Demo: Autofac Container with ASP .NET Core Web Application

By the way, did you see that the scoped and transient objects are getting created at exactly the same time. Can you guess why ?

Let me know your thoughts in the comments section.

Leave a ReplyCancel reply