简体   繁体   English

如何将 Autofac 与 Asp.net core 2.2 集成

[英]How to integrate Autofac with Asp.net core 2.2

I have follow guide at: https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html我有以下指南: 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但是,在 Asp Net core 2.2 的最新版本中,函数 ConfigureServices(IServiceCollection services) 返回 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:在您的解决方案中,您在 ConfigureServices 方法中使用了void返回类型:

public void ConfigureServices(IServiceCollection services)

Actually you can setup and return IServiceProvider :其实你可以设置并返回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官方文档中查看更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM