autofac/Autofac.AspNetCore

Support Generic Hosts in ASP.NET Core 2.1

Closed this issue ยท 7 comments

ASP.NET Core 2.1 has introduced Generic Hosts with the new IHostBuilder abstraction which allows configuring a custom container.

It would be nice to have a UseAutofac extension on IHostBuilder as we have on IWebHostBuilder that abstracts the following

var host = new HostBuilder()
    .UseServiceProviderFactory<ServiceContainer>(new SerivceContainerFactory())
    .ConfigureContainer<ServiceContainer>((hostContext, container) =>
    {
    })

I'd be interested in ways we can do something like this and still support pre-ASP.NET-2.1 clients. Maybe @alexmg knows a trick. Problem is we can do conditional compilation on .NET framework but not really based on target dependency versions.

I ran into a similar issue in the multitenant ASP.NET Core support where they made a breaking change in the ASP.NET Core framework but I got around it with some fancy reflection. It wasn't a change in the public Autofac API that should only show up based on the ASP.NET Core you're on.

I'd hate to have to maintain a different integration package for every ASP.NET Core version just to support new convenience extension methods.

I have created a sample of using Autofac with the new .NET Generic Host.

https://github.com/autofac/Examples/tree/master/src/GenericHostBuilderExample

I don't think there is anything special we have to do here as everything required is in the Autofac.Extensions.DependencyInjection package.

That sample builds and runs but it hangs on a thread and does not close down completely. I am unable to get autofac working properly without hanging on console shutdown.

Same issue as brianw2,

Adding UseConsoleLifetime() doesn't help.

Same issue for me, in plain .net core console project.
Console just hangs on shutdown when UseServiceProviderFactory used

@Thumann, @alexmg, and @alex-namely -- did you ever come up with a more generic fix for this? I'm not using Hangfire but am having the same problem -- the console hangs on shutdown.

Updating to the latest Autofac (4.8.1) resolved this for me.