Autofac Integration

Autofac is one of the most used dependency injection frameworks for .NET. It provides advanced features compared to .Net Core's standard DI library, like dynamic proxying and property injection.

Install Autofac Integration

All the startup templates and samples are Autofac integrated. So, most of the time you don't need to manually install this package.

If you're not using a startup template, you can use the ABP CLI to install it to your project. Execute the following command in the folder that contains the .csproj file of your project (suggested to add it to the executable/web project):

abp add-package Volo.Abp.Autofac

If you haven't done it yet, you first need to install the ABP CLI. For other installation options, see the package description page.

Finally, configure AbpApplicationCreationOptions to replace default dependency injection services by Autofac. It depends on the application type.

ASP.NET Core Application

Call UseAutofac() in the Program.cs file as shown below:

public class Program
{
    public static int Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    internal static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseAutofac(); //Integrate Autofac!
}

If you are using the static WebApplication class, you can call the UseAutofac() extension method as shown below:

public class Program
{
    public async static Task Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Host.UseAutofac(); // Integrate Autofac!
        await builder.AddApplicationAsync<MyProjectNameWebModule>();
        var app = builder.Build();
        await app.InitializeApplicationAsync();
        await app.RunAsync();
    }
}

Console Application

Call UseAutofac() method in the AbpApplicationFactory.Create options as shown below:

using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;

namespace AbpConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var application = AbpApplicationFactory.Create<AppModule>(options =>
            {
                options.UseAutofac(); //Autofac integration
            }))
            {
                //...
            }
        }
    }
}

Using the Autofac Registration API

If you want to use Autofac's advanced registration API, you need to access the ContainerBuilder object. Volo.Abp.Autofac nuget package defines the IServiceCollection.GetContainerBuilder() extension method to obtain the ContainerBuilder object.

Example: Get the ContainerBuilder object in the ConfigureServices method of your module class

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var containerBuilder = context.Services.GetContainerBuilder();
    containerBuilder.RegisterType<MyService>(); // Using Autofac's registration API
}

You should install the Volo.Abp.Autofac nuget package to the project that you want to use the Autofac API.

Contributors


Last updated: June 14, 2023 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

Building Modular Monolith Applications Using .NET and ABP Framework

17 Oct, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More