Using Autofac with HostApplicationBuilder

58 Views Asked by At

I am building a new .net 8 worker service and microsoft has modified the generic host. MSDN

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();

instead of the older

IHostBuilder builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
    services.AddHostedService<Worker>();
});

The issue being that I can't seem to find the correct syntax for registering autofac as the service factory on a HostApplicationbuilder instead of an IHostbuilder as shown here Autofac Documentation

I have a working asp.net 8 project that works with the new WebApplicationBuilder

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureContainer<ContainerBuilder>(builder => builder.RegisterModule(new AutofacBuilder(settings)));

and I would like to use a similar pattern for this console worker application.

Can anyone provide advice on the correct way to register an Autofac and an Autofac Module as the service provider for a HostApplicationBuilder?

1

There are 1 best solutions below

0
Mindbane On

Autofac can be registered as the ServiceProviderFactory for a HostApplicationBuilder via:

builder.ConfigureContainer(new AutofacServiceProviderFactory(), builder => builder.RegisterModule(new AutofacBuilder(settings)));