AutoFac container configuration (without ConfigureContainer) fails as of ASP.NET Core 3.0 Preview 3 (3.0.100-preview3-010431)
Getting Error:
ConfigureServices returning an System.IServiceProvider isn't supported.
Stack Trace:
at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Program.Main(String[] args) in Program.cs:line 13
As it is outlined in the Quick Start (Without ConfigureContainer) at https://docs.autofac.org/en/latest/integration/aspnetcore.html#quick-start-without-configurecontainer it is suggested for ASP.NET Core 1.0, unlike with ConfigureContainer for
ASP.NET Core 1.1 and later.
ASP.NET Core 1.1 introduced the ability to have a strongly-typed container configuration. It provides a ConfigureContainer method where you register things with Autofac separately from registering things with the ServiceCollection.
What's the new preferred way to register AutoFac?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hello @IvanFarkas ... We will be updating docs for 3.0 when the Release Candidate arrives. Your best bet right now is to ask on a support forum, such as Stack Overflow or for Autofac, or a support chat, such as Slack or Gitter. Leave this issue open, and we will use it to track the work. If u gain insight elsewhere on Autofac for 3.0, please comment here to crosslink what u find. It can definitely speed things up when we get to this issue. Alternatively, you're welcome to submit a PR with the updates ... Ping me if u need guidance on how we handle versioning.
How to solve the problem?
@cqinwn I've marked this for the Product Unit ("PU") to take a look at. For a faster answer, ask on a support forum (for example, Stack Overflow) or support chat where devs who use Autofac hang out, or try contacting the Autofac maintainers directly.
Stack Overflow: Configure AutoFac in ASP.NET Core 3.0 Preview 5 or higher
cc: @davidfowl
I resolved it by using With ConfigureContainer configuration
I also had to add the following to Program.CreateHostBuilder thanks to How to integrate Autofac in ASP.NET Core generic hosts article by Mickaël Derriey.
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
Program.cs
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>();
});
}
one more question, i think the .net core 3.0 framework will build container automatically, so how can get the container directly from my code?
i want to implement some static class like "IocManager..." something.
@IvanFarkas
Add below code in your startup.cs Configure:
IocManager.Instance.Container = app.ApplicationServices.GetAutofacRoot();
one more question, i think the .net core 3.0 framework will build container automatically, so how can get the container directly from my code?
i want to implement some static class like "IocManager..." something.
@IvanFarkas
Most helpful comment
I resolved it by using With ConfigureContainer configuration
I also had to add the following to Program.CreateHostBuilder thanks to How to integrate Autofac in ASP.NET Core generic hosts article by Mickaël Derriey.
Program.cs