// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { // We want to run the API and MVC UI app on a single ASP.NET Core host but we cannot get along with a single DI container currently // as endpoint routing is bugged regarding pipeline branches (https://github.com/dotnet/aspnetcore/issues/19330), // so we need two application branches with isolated MVC-related and shared common services (like service layer services). // Unfortunately, the built-in DI is insufficient for this setup, we need some more advanced solution like Autofac. // As a matter of fact, Autofac could handle this situation out-of-the-box (https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#multitenant-support) // but this approach also has issues currently (https://github.com/autofac/Autofac.AspNetCore.Multitenant/issues/27) // The services defined here go into the root DI container and are accessible to the nested (tenant) containers. // 1. register shared services and obtain options necessary for DI configuration using (var optionsProvider = ApiStartup.BuildImmediateOptionsProvider(ConfigureImmediateOptions)) { ApiStartup.ConfigureBaseServices(services, optionsProvider); UIOptions = optionsProvider.GetRequiredService <IOptions <UIOptions> >().Value; } ConfigureOptions(services); ConfigureServicesPartial(services); // 2. register the tenants services.AddSingleton(new Tenants( new ApiTenant(ApiTenantId, this, typeof(Api.Startup).Assembly), new UITenant(UITenantId, this, typeof(Startup).Assembly))); // 3. finally register a startup filter which ensures that the main branch is set up before any other middleware added services.Insert(0, ServiceDescriptor.Transient <IStartupFilter, MainBranchSetupFilter>()); }
public Startup(IConfiguration configuration, IWebHostEnvironment environment) { Configuration = configuration; Environment = environment; ApiStartup = new Api.Startup(Configuration, Environment, provideRazorTemplating: false); UIOptions = new UIOptions(); }