相关疑难解决方法(0)

将 Autofac 与 ASP.Net Core 3.1 通用主机“Worker Service”应用程序一起使用

在 ASP.Net Core 应用程序中,使用以下命令可以轻松配置 Autofac:

public class Program
{
  public static void Main(string[] args)
  {
    // ASP.NET Core 3.0+:
    // The UseServiceProviderFactory call attaches the
    // Autofac provider to the generic hosting mechanism.
    var host = Host.CreateDefaultBuilder(args)
        .UseServiceProviderFactory(new AutofacServiceProviderFactory())
        .ConfigureWebHostDefaults(webHostBuilder => {
          webHostBuilder
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>();
        })
        .Build();

    host.Run();
  }
}

public class Startup
{
  // Omitting extra stuff so you can see the important part...
  public void ConfigureServices(IServiceCollection services)
  {
    // Add controllers as services so they'll be resolved. …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection autofac .net-core asp.net-core

7
推荐指数
1
解决办法
7716
查看次数