简体   繁体   English

在ASP.NET Core 2.2中使用Autofac

[英]Using autofac in asp.net core 2.2

I am trying to use latest Autofac with asp.net core 2.2 project. 我正在尝试将最新的Autofac与asp.net core 2.2项目一起使用。 However documentation of autofac seems to be outdated. 但是,autofac的文档似乎已过时。 I am trying to use autofac without ConfigureContainer: 我正在尝试在没有ConfigureContainer的情况下使用autofac:

// ConfigureServices is where you register dependencies. This gets
  // called by the runtime before the Configure method, below.
  public IServiceProvider ConfigureServices(IServiceCollection services)
  {
    // Add services to the collection.
    services.AddMvc();

    // Create the container builder.
    var builder = new ContainerBuilder();

    // Register dependencies, populate the services from
    // the collection, and build the container.
    //
    // Note that Populate is basically a foreach to add things
    // into Autofac that are in the collection. If you register
    // things in Autofac BEFORE Populate then the stuff in the
    // ServiceCollection can override those things; if you register
    // AFTER Populate those registrations can override things
    // in the ServiceCollection. Mix and match as needed.
    builder.Populate(services);
    builder.RegisterType<MyType>().As<IMyType>();
    this.ApplicationContainer = builder.Build();

    // Create the IServiceProvider based on the container.
    return new AutofacServiceProvider(this.ApplicationContainer);
  }

but the ConfigureServices method signature is different in asp.net core 2.2 但是在ASP.NET Core 2.2中,ConfigureServices方法签名不同

public void ConfigureServices(IServiceCollection services)
{
}

there is not return type. 没有返回类型。 Anyone knows how to use autofac in asp.net core 2.2? 有谁知道如何在asp.net core 2.2中使用autofac?

You need to use this package: 您需要使用此软件包:

Autofac.Extensions.DependencyInjection Autofac.Extensions.DependencyInjection

In your program.cs, just use these lines of code: 在您的program.cs中,只需使用以下代码行:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureServices(services => services.AddAutofac());
    }

In your startup.cs, create a method called 在您的startup.cs中,创建一个名为

    public void ConfigureContainer(ContainerBuilder builder)
    {
    }

And use "builder" to register whatever you want. 并使用“生成器”注册所需的任何内容。 Autofac will find this method itself. Autofac会自己找到此方法。

You don't need to call any builder.Build() anymore. 您不再需要调用任何builder.Build()了。

Following remarks in order to execute reccurent code with injected values, you need to add : 为了执行带有注入值的递归代码,在注释之后,您需要添加

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddHostedService<MyHostedService>();
    ...
}

public class MyHostedService : IHostedService
{
    private Timer _timer;
    private IInjectedService _myInjectedService;

    public MyHostedService(IServiceProvider services)
    {
        var scope = services.CreateScope();

        _myInjectedService = scope.ServiceProvider.GetRequiredService<IInjectedService>();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5));

        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        _timer?.Change(Timeout.Infinite, 0);

        return Task.CompletedTask;
    }

    private async void DoWork(object state)
    {
        _myInjectedService.DoStuff();
    }
}

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

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