我有以下指南:https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html
在最后一步,它显示:
// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);
但是,Asp Net core 2.2 的最新版本,函数ConfigureServices(IServiceCollection services) 返回void
public void ConfigureServices(IServiceCollection services)
如何根据最新更改重构我的代码?
在您的解决方案中,您在 ConfigureServices 方法中使用了 void
返回类型:
public void ConfigureServices(IServiceCollection services)
实际上你可以设置并返回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);
}
在 official documentation 中查看更多详细信息
我是一名优秀的程序员,十分优秀!