Steve
Steve

Reputation: 4453

Setup Autofac in .NET Core 3 and 3.1

From the official Autofac documentation for ASP.NET Core 3.0 (I am using .NET Core 3.1), the startup constructor parameter type in the official documentation is IHostingEnvironment

public Startup(IHostingEnvironment env)
  {
    // In ASP.NET Core 3.0 `env` will be an IWebHostEnvironment, not IHostingEnvironment.
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    this.Configuration = builder.Build();
  }

while mine is IConfiguration

 public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

        } 

How do I setup for this method then in .NET Core 3.1? or Autofac has not yet release documentation for .NET Core 3.1?

Upvotes: 0

Views: 638

Answers (1)

talesa82035
talesa82035

Reputation: 124

There're two step to setup your autofac in .NET Core3.1.

First,you need to write below in Program.cs.

Host.CreateDefaultBuilder(args)
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureWebHostDefaults(webBuilder =>
    {
      webBuilder.UseStartup<Startup>();
    });

Then,create a new method in Startup.cs.

public void ConfigureContainer(ContainerBuilder builder)
{
    builder.RegisterAssemblyTypes(Assembly.Load("")).AsImplementedInterfaces();
}

Upvotes: 1

Related Questions