I have follow guide at: https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html
At last step, it show:
// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);
However, last version of Asp Net core 2.2, the function ConfigureServices(IServiceCollection services) is return void
public void ConfigureServices(IServiceCollection services)
How can I refactor my code follow latest change ?
In your solution you used void return type in ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
Actually you can setup and return IServiceProvider:
public class Startup
{
public IContainer Container { get; private set; }
// ...
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// create new container builder
var containerBuilder = new ContainerBuilder();
// populate .NET Core services
containerBuilder.Populate(services);
// register your autofac modules
containerBuilder.RegisterModule(new ApiModule());
// build container
Container = containerBuilder.Build();
// return service provider
return new AutofacServiceProvider(Container);
}
See more details in official documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With