0

I am trying to migrate our framework project to .NET Core 3.1. As part of the migration, I am trying to register modules via ConfigureContainer method provided by the GenericHost. This is what I have:

Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder => builder.RegisterModule(new 
                         WorkerServiceDependencyResolver.WorkerServiceDependencyResolver()))

And my WorkerServiceDependencyResolver has the following:

builder.RegisterModule(new FirstModule());
builder.RegisterModule(new SecondModule());

But when I do it this way, my application doesn't run, it starts without any error, but doesn't do anything.

But If I write it this way (this is how we had in .NET Framework):

var builder = new ContainerBuilder();
builder.RegisterModule(new FirstModule());
builder.RegisterModule(new SecondModule());
_container = builder.Build();

Everything works as expected when I explicitly build the container, but my understanding was that we do not need that in .NET Core?

Any inputs are greatly appreciated.

Cheers!

Minu
  • 232
  • 4
  • 13
  • You can refer to this: https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html – LouraQ May 01 '20 at 06:30
  • I am looking for incorporating into .NET Core self hosted console app and also I have given as per the steps mentioned, but doesn't seem to register successfully without building the container explicitly @YongqingYu – Minu May 04 '20 at 02:00

1 Answers1

1

By specifying to the IHostBuilder that the Service Provider Factory is an AutofacServiceProviderFactory, it allows you create to a method right inside your Startup class, called ConfigureContainer which takes the ContainerBuilder as a parameter.

This is how you would instantiate the IHostBuilder. Nothing fancy, just following the ASP NET Core guide.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseServiceProviderFactory(new AutofacServiceProviderFactory())
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Then, in the Startup class, add that method.

public class Startup
{
    public Startup(IConfiguration configuration) => Configuration = configuration;

    public IConfiguration Configuration { get; }

    // ...

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Here you register your dependencies
        builder.RegisterModule<FirstModule>();
    }

    // ...
}

Then, your IContainer will be the IOC Container of your ASPNET Core application scope.
Notice than the builder is not being built at any time. This will be done by the IHostBuilder at some point (making use of the Autofac extension).

Hope that helps.

VRoxa
  • 973
  • 6
  • 25
  • Hi @VRoxa, Mine is a Console application and not an ASP.NET Core application. I have followed the same steps in mine, if you see the post, but that doesn't seem to work. – Minu May 07 '20 at 16:29
  • @vroxa When do you register something in the ConfigureContainer with Autofac vs. registering a singleton in the ConfigureServices method? – Mike Lenart Jun 01 '21 at 16:14