How to integrate Autofac in ASP.NET Core generic hosts
02 Aug 2018ASP.NET Core 2.1 brought a new feature that is generic hosts. They allow to write apps that rely on ASP.NET Core concepts like logging, configuration and built-in DI but that are not web applications.
I was playing with them yesterday and wanted to see if I could easily integrate the Autofac IoC container with it. After looking at the ASP.NET Core integration page in the Autofac docs, I came up with code that looks like the following:
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
internal class Program
{
public static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureServices(services => services.AddAutofac())
.ConfigureContainer<ContainerBuilder>(builder =>
{
// registering services in the Autofac ContainerBuilder
})
.UseConsoleLifetime()
.Build()
.RunAsync();
}
}
This all looks pretty straightforward and follows the docs, but at runtime the application threw an exception with the following error message:
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' to type 'Autofac.ContainerBuilder'.'
That’s interesting, given:
services.AddAutofac()
registers anAutofacServiceProviderFactory
instance asIServiceProviderFactory
as we can see here; and- the code tells us that the
CreateBuilder
method ofAutofacServiceProviderFactory
returns an instance ofContainerBuilder
So we’re all good, right?! What’s wrong?! Interestingly, I also read Andrew Lock’s post about the differences between web host and generic host yesterday, and thought maybe something was fooling us into thinking we were doing the right thing.
So I cloned the aspnet/Hosting
repo, checked out the 2.1.1
tag, opened the solution in Visual Studio, and started readong through the HostBuilder.cs
file.
And there it was: the HostBuilder
class uses a ServiceProviderAdapter
that wraps the IServiceProviderFactory
. This means that registering an IServiceProviderFactory
like services.AddAutofac()
does conveys no meaning to a HostBuilder
.
Luckily, while going through the code, I also found the UseServiceProviderFactory
method on the HostBuilder
class. The difference is that this one wraps the provided factory within the adapter.
The code then became:
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
internal class Program
{
public static async Task Main(string[] args)
{
await new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
// registering services in the Autofac ContainerBuilder
})
.UseConsoleLifetime()
.Build()
.RunAsync();
}
}
And it worked!
I don’t know why the generic host uses an adapter around the service provider factory — I asked the question on Twitter, time will tell if we get the answer.
The morale here is very close to the one in Andrew’s post: don’t assume everything you know about web host is true or will work with generic host.