Search code examples
asp.net-coreautofac

Upgrade Autofac Multitenant with ASP.NET Core 2.2 to ASP.NET Core 5


I have an ASP.NET Core 2.2 application that is currently using following dependencies:

  • Autofac.AspNetCore.Multitenant version 1.1.0
  • Autofac.AspNetCore.Extensions version :1.0.2

I want to upgrade the application from from ASP.NET Core 2.2 to ASP.NET 5. Because of the new Generic IHostBuilder and using AutofacServiceProviderFactory, I am kind of lost as to how to go about building tenants in program.cs and add/remove at runtime. In .NET Core 2.2 I am doing it as follows:

var webHost = CreateTenantWebHostBuilder(args)
    .UseAutofacMultiTenant()
    .UseStartup<Startup>()
    .Build();

using (var scope = webHost.Services.CreateScope())
{
    try
    {
        var applicationLifetime = scope.ServiceProvider
            .GetRequiredService<IApplicationLifetime>();
        var tenantStore = scope.ServiceProvider
            .GetRequiredService<ICustomTenantsStore>();

        await tenantStore.InitializeTenantsAsync(applicationLifetime.ApplicationStopping);
    }
    catch (Exception ex)
    {
                       
    }
}

await webHost.RunAsync(cancelTokenSource.Token);

Solution

  • I would recommend taking some time to review some of the resources available.

    You're making quite a jump from .NET Core 2.2 to 5.0, so you're not going to be able to stay on the older Autofac versions. I'd recommend updating to the latest - you'll get updated compatibility support for .NET 5, bug fixes, some perf improvements, and better troubleshooting support (among other things in the latest Autofac and extensions).