I need ti use this AutoFac
in ASP core 3.0
When I use this code in startu up:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();
return services.BuildAutofacServiceProvider();
}
It show me this error:
'ConfigureServices returning an System.IServiceProvider isn't supported.'
And I change the program.cs by this:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
But it not solved.
This is BuildAutofacServiceProvider()
Code:
public static IServiceProvider BuildAutofacServiceProvider(this IServiceCollection services)
{
var ContainerBuilder = new ContainerBuilder();
ContainerBuilder.Populate(services);
ContainerBuilder.AddService();
var container = ContainerBuilder.Build();
return new AutofacServiceProvider(container);
}
How can I solve this problem?
IServiceProvider
when they switched to use the generic application host. Using a service provider factory is now the only way to integrate any third-party DI container. – Deadbeat