25

I am trying to use autofac in my dotnet core 3.1 project, but I am unable to run project after writing ConfigureContainer inside the startup.cs file.

    public void ConfigureContainer(ContainerBuilder builder)
    {

        var databaseConnectionString = Configuration.GetConnectionString("Database");

        builder.RegisterModule(new MediatorModule());
        builder.RegisterModule(new ApplicationModule(databaseConnectionString));
    }

The error I am getting is :

System.InvalidCastException: Unable to cast object of type
'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Autofac.ContainerBuilder'. at Microsoft.Extensions.Hosting.Internal.ConfigureContainerAdapter`1.ConfigureContainer(HostBuilderContext hostContext, Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at Program.Main(String[] args) in C:\src\Program.cs:line 39

Benjamin
  • 3,499
  • 8
  • 44
  • 77
  • 1
    Is there a particular reason to use Autofac in the first place? .Net core supports native dependency injection – Emiliano Javier González Jan 10 '20 at 11:16
  • 3
    I had the same error when migrated from WebHostBuilder to generic HostBuilder and used `return Host.CreateDefaultBuilder(args).ConfigureServices(p => p.AddAutofac())`. Earlier it worked for `WebHost.CreateDefaultBuilder(args).ConfigureServices(p => p.AddAutofac())`. The correct way to fix this is like @Cyril Durand answered. In short: use `.UseServiceProviderFactory(new AutofacServiceProviderFactory())`. My problem is described well here: https://mderriey.com/2018/08/02/autofac-integration-in-asp-net-core-generic-hosts/ – flam3 Apr 28 '20 at 07:50
  • @EmilianoJavierGonzález because it lets you do things that the built in IoC doesn't – Luke Feb 02 '22 at 23:07

1 Answers1

51

When you configure your host you should call UseServiceProviderFactory(new AutofacServiceProviderFactory())

public static void Main(string[] args)
{
    // ASP.NET Core 3.0+:
    // The UseServiceProviderFactory call attaches the
    // Autofac provider to the generic hosting mechanism.
    var host = Host.CreateDefaultBuilder(args)
                   .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                   .ConfigureWebHostDefaults(webHostBuilder => {
                       webHostBuilder
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .UseIISIntegration()
                        .UseStartup<Startup>();
                   })
                   .Build();

    host.Run();
}

Without this, .net core will create a ServiceCollection instead of a ContainerBuilder and an InvalidCastException will be thrown.

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
  • Thanks a lot, '.UseServiceProviderFactory(new AutofacServiceProviderFactory())' was missing in my 'HostBuilderConfig'. – Benjamin Jan 10 '20 at 13:46
  • These hosting changes with using service provider factory are acceptable only for ASP.NET Core 3.0+ versions – Damir Beylkhanov Jul 30 '20 at 14:21
  • nice answer. for a further usage everyone should refer to a MSDN -> https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0#custom-dependency-injection-di-container – Zenka Apr 07 '22 at 22:17