2

https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting

It looks like Autofac only supports the generic hosting API, IHostBuilder. I wonder if the old asp netcore 2.x documentation is still relevant to asp netcore 3 applications.

Also, I found https://github.com/autofac/Autofac.AspNetCore has not been updated for a long time, so I guess Autofac has no intention to support IWebHostBuilder in the future...

Do we have any guideline about how to set up Autofac in AspNetCore 3.x using the IWebHostBuilder API?

I read about [this][https://stackoverflow.com/questions/59980827/service-fabric-aspnet-core-3-1-autofac-webhostbuilder] post, and it does not answer my question.

David S.
  • 10,578
  • 12
  • 62
  • 104

2 Answers2

1

In ASP.NET Core 3.x they changed how dependency injection integrates and, no, the version 2.x documentation no longer applies - either in the Autofac case or in the ASP.NET case. ASP.NET Core intentionally shifted to the generic hosting model, where the web host is a layer on top of that.

It's not that Autofac "has no intention of supporting IWebHostBuilder", it's that that's not an option in ASP.NET Core 3. It changed at the framework level; that's not how you integrate with ASP.NET Core anymore. You don't attach the DI factory to the web host anymore, you attach it to the outer generic host.

You do register things in your Startup class just like in ASP.NET Core 2.

The docs you linked to explain all of that and show examples. You can also see in the Microsoft ASP.NET Core 2 to 3 migration docs that HostBuilder replaces WebHostBuilder; and that WebHostBuilder, while it might still exist, is being deprecated and you shouldn't use it.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
0

You can create constructor parameters have IWebHostEnvironment on the Startup class and asp.net core will auto inject IWebHostEnvironment.

Register IWebHostBuilder as instance on the ConfigureContainer.

See the below code.

public class Startup
{     
    private readonly IWebHostEnvironment _environment;

    // Auto injection IWebHostEnvironment
    public Startup(IWebHostEnvironment environment)
    {        
        _environment = environment;
    }
     
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
    }

    // Autofac ID
    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Register your own things directly with Autofac, like:
        builder.Register<SampleClass>();
        
        // Register your IWebHostEnvironment
        builder.RegisterInstance(_environment);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...
    }
}

public class SampleClass
{
    private readonly IWebHostEnvironment _environment;

    // If you register IWebHostEnvironment on the Startup, IWebHostEnvironment will auto inject.
    public SampleClass(IWebHostEnvironment environment)
    {
        _environment = environment;
    }
}

When you resolve the SampleClass, you can see the IWebHostEnvironment is auto injected to constructor.

Changemyminds
  • 1,147
  • 9
  • 25