title: Adding Autofac to .NET core 6.0 using the new single file template
body: <p>I am trying to add Autofac to a .Net 6.0 web API.
  I'm using the latest ASP.NET Core Web API template that generates a single start-up Program.cs file.</p>
  <p>Installed Autofac versions:</p>
  <pre><code>Autofac 6.3.0
  Autofac.Extensions.DependancyInjection (7.2.0-preview.1)
  </code></pre>
  <p>Installed .Net 6.0 versions:</p>
  <pre><code>Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5
  Microsoft.WindowsDesktop.App 6.0.0-rc.2.21501.6
  </code></pre>
  <p>Just in case of doubt, this is the entire content of the Program.cs file (yes, no namespaces or class definition. Only a Program.cs file and no StartUp class or Startup.cs file)</p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  
  // Add services to the container.
  
  builder.Services.AddControllers();
  // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  
  var app = builder.Build();
  
  // Configure the HTTP request pipeline.
  if (app.Environment.IsDevelopment())
  {
      app.UseSwagger();
      app.UseSwaggerUI();
  }
  
  app.UseHttpsRedirection();
  
  app.UseAuthorization();
  
  app.MapControllers();
  
  app.Run()
  </code></pre>
  <p>I've tried looking through the latest Autofac documentation but the examples there, despite saying for .Net Core 3.x and later don't seem to fit with .Net 6.0 code. I can't figure out how to add Autofac into the middleware pipeline.</p>
  <p>Any help is greatly appreciated.</p>
  <p>Code snippet from Autofac website</p>
  <pre><code>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 =&gt; {
            webHostBuilder
              .UseContentRoot(Directory.GetCurrentDirectory())
              .UseIISIntegration()
              .UseStartup&lt;Startup&gt;();
          })
          .Build();
  
      host.Run();
    }
  }
  </code></pre>
  <p>Autofac documention:</p>
  <p><a href="https://docs.autofac.org/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting" rel="noreferrer">https://docs.autofac.org/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting</a></p>