C# 在asp.net core 2.2中使用autofac

C# 在asp.net core 2.2中使用autofac,c#,.net,asp.net-core,.net-core,autofac,C#,.net,Asp.net Core,.net Core,Autofac,我正在尝试将最新的Autofac与asp.net core 2.2项目一起使用。然而,autofac的文档似乎已经过时。 我尝试在没有配置容器的情况下使用autofac: // ConfigureServices is where you register dependencies. This gets // called by the runtime before the Configure method, below. public IServiceProvider Configur

我正在尝试将最新的Autofac与asp.net core 2.2项目一起使用。然而,autofac的文档似乎已经过时。 我尝试在没有配置容器的情况下使用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);
  }
没有返回类型。
有人知道如何在asp.net core 2.2中使用autofac吗?

您需要使用此软件包:

Autofac.Extensions.DependencyInjection

在program.cs中,只需使用以下代码行:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureServices(services => services.AddAutofac());
    }
并使用生成器注册您想要的任何内容。Autofac将自行找到此方法

你不需要打电话给任何建设者。再建设者

以下备注为了使用注入值执行reccurent代码,您需要添加:


ConfigureServices是一种约定方法,您可以将其更改为返回服务提供程序,它仍然可以工作。即使是文档也说明了如何在没有ConfigureContainer的情况下进行配置,OP明确表示他们希望避免在我的应用程序中使用ConfigureContainer。我有一个每五分钟运行一次的服务。我如何解决它?它不是任何控制器的一部分。它是每n分钟运行一次的单例服务。使用您上面提到的方法,我无法做到这一点,因为我没有对IContainer的引用object@pantonis刚刚添加了使用注入的代码,这要感谢没有访问IContainer@hugo当存在HostedService时,会出现竞争条件,因为它们已添加到ConfigureServices,并且已添加我的autofac寄存器到在ConfigureServices之后调用的ConfigureContainer。@Pantoni是在ConfigureContainer之前调用ConfigureServices,但至少不是在创建HostedService之前,我对dotnet core 2.x下的项目没有问题
    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureServices(services => services.AddAutofac());
    }
    public void ConfigureContainer(ContainerBuilder builder)
    {
    }
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();
    }
}