I have an ASP.NET Core 2.2 application that is currently using following dependencies:
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);
I would recommend taking some time to review some of the resources available.
Autofac.AspNetCore.Multitenant
repo has samples which show how to deal with registering the multitenant container factory, setting up common dependencies, and using a static method to configure tenants.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).