10

In an ASP.Net Core application, its easy to configure Autofac using:

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.
    services.AddMvc().AddControllersAsServices();
  }

  public void ConfigureContainer(ContainerBuilder builder)
  {
    // If you want to set up a controller for, say, property injection
    // you can override the controller registration after populating services.
    builder.RegisterType<MyController>().PropertiesAutowired();
  }
}

But in a generic host, the Worker class does not have any built in support for ConfigureServices and ConfigureContainer.

How do I enable the same for a generic host in a non ASP.Net Core application?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Satyajit
  • 1,971
  • 5
  • 28
  • 51

2 Answers2

25

The generic host builder does have built-in support for ConfigureContainer and ConfigureServices

var host = Host.CreateDefaultBuilder(args)
    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureContainer<ContainerBuilder>(builder => {

        builder.RegisterType<MyDependencyType>();

        //...
    })
    .ConfigureServices(services => {

        services.AddHostedService<Worker>();

        //...
    })
    .ConfigureWebHostDefaults(webHostBuilder => {
        webHostBuilder
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>();
    })
    .Build();

host.Run();

Reference .NET Generic Host

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • How would you call UseStartup in this case? – Maulik Modi Aug 11 '21 at 12:26
  • 1
    @MaulikModi check [docs here](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0#the-startup-class). It would be called within the `ConfigureWebHostDefaults` extension – Nkosi Aug 11 '21 at 12:59
0

Adding to @Nkosi's excellent answer:

If you also need to access some config data from ConfigureContainer, use the (hostContext, services) overload:

    // ...    
    .ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
          {
             IConfiguration configuration = hostContext.Configuration;
             var myDbConnectionString = configuration.GetConnectionString("MyDbConnection");

             builder.RegisterType<MyDependencyType>();
          }))
    // ... 
brunochaina
  • 719
  • 7
  • 7