コード例 #1
0
        public async Task InitializeAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting tenant initialization");

            try
            {
                foreach (var kvp in _options.Tenants)
                {
                    _logger.LogInformation("Starting initialization for {tenant}", kvp.Key);

                    var tenantConfiguration = _tenantConfigurations.FirstOrDefault(i => i.TenantId.ToString() == kvp.Key);

                    var defaultBuilder = new TenantBuilder(kvp.Key);
                    foreach (var configureTenantsDelegate in _options.ConfigureTenantsDelegates)
                    {
                        configureTenantsDelegate(defaultBuilder);
                    }

                    var tenantBuilder = new TenantBuilder(kvp.Key);
                    if (kvp.Value != null)
                    {
                        kvp.Value(tenantBuilder);
                    }

                    var options = new TenantBuilder(kvp.Key)
                    {
                        ConfigureAppConfigurationDelegate = (context, builder) =>
                        {
                            defaultBuilder.ConfigureAppConfigurationDelegate(context, builder);
                            tenantBuilder.ConfigureAppConfigurationDelegate(context, builder);
                            if (tenantConfiguration != null)
                            {
                                tenantConfiguration.ConfigureAppConfiguration(context, builder);
                            }
                        },
                        ConfigureServicesDelegate = (context, services) =>
                        {
                            defaultBuilder.ConfigureServicesDelegate(context, services);
                            tenantBuilder.ConfigureServicesDelegate(context, services);
                            if (tenantConfiguration != null)
                            {
                                tenantConfiguration.ConfigureServices(context, services);
                            }
                        },
                        InitializeDbAsyncDelegate = async(sp, ct) =>
                        {
                            await defaultBuilder.InitializeDbAsyncDelegate(sp, ct);

                            await tenantBuilder.InitializeDbAsyncDelegate(sp, ct);

                            if (tenantConfiguration != null)
                            {
                                await tenantConfiguration.InitializeDbAsync(sp, ct);
                            }
                        },
                        InitializeAsyncDelegate = async(sp, ct) =>
                        {
                            await defaultBuilder.InitializeAsyncDelegate(sp, ct);

                            await tenantBuilder.InitializeAsyncDelegate(sp, ct);

                            if (tenantConfiguration != null)
                            {
                                await tenantConfiguration.InitializeAsync(sp, ct);
                            }
                        },
                    };

                    var serviceProvider = new AutofacServiceProvider(_mtc.GetTenantScope(kvp.Key));

                    try
                    {
                        await options.InitializeDbAsyncDelegate(serviceProvider, cancellationToken);

                        await options.InitializeAsyncDelegate(serviceProvider, cancellationToken);

                        _logger.LogInformation("Initialization for {tenant} completed", kvp.Key);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Initialization for {tenant} failed", kvp.Key);
                        throw;
                    }
                }

                _logger.LogInformation("tenant initialization completed");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "tenant initialization failed");
                throw;
            }
        }
            private void ConfigureTenants(MultitenantContainer mtc)
            {
                var serviceProvider = new AutofacServiceProvider(mtc);

                var hostingEnvironment   = serviceProvider.GetRequiredService <IHostEnvironment>();
                var config               = serviceProvider.GetRequiredService <IConfiguration>();
                var tenantConfigurations = serviceProvider.GetServices <ITenantConfiguration>();

                if (_options.AutoAddITenantConfigurationTenants)
                {
                    foreach (var tenantId in tenantConfigurations.Select(i => i.TenantId.ToString()))
                    {
                        if (!_options.Tenants.ContainsKey(tenantId))
                        {
                            _options.Tenants.Add(tenantId, null);
                        }
                    }
                }

                foreach (var kvp in _options.Tenants)
                {
                    var tenantConfiguration = tenantConfigurations.FirstOrDefault(i => i.TenantId.ToString() == kvp.Key);

                    var actionBuilder  = new ConfigurationActionBuilder();
                    var tenantServices = new ServiceCollection();

                    var defaultBuilder = new TenantBuilder(kvp.Key);

                    foreach (var ConfigureTenantsDelegate in _options.ConfigureTenantsDelegates)
                    {
                        ConfigureTenantsDelegate(defaultBuilder);
                    }

                    var tenantBuilder = new TenantBuilder(kvp.Key);
                    if (kvp.Value != null)
                    {
                        kvp.Value(tenantBuilder);
                    }

                    var options = new TenantBuilder(kvp.Key)
                    {
                        ConfigureAppConfigurationDelegate = (context, builder) =>
                        {
                            defaultBuilder.ConfigureAppConfigurationDelegate(context, builder);
                            tenantBuilder.ConfigureAppConfigurationDelegate(context, builder);
                            if (tenantConfiguration != null)
                            {
                                tenantConfiguration.ConfigureAppConfiguration(context, builder);
                            }
                        },
                        ConfigureServicesDelegate = (context, services) =>
                        {
                            defaultBuilder.ConfigureServicesDelegate(context, services);
                            tenantBuilder.ConfigureServicesDelegate(context, services);
                            if (tenantConfiguration != null)
                            {
                                tenantConfiguration.ConfigureServices(context, services);
                            }
                        },
                        InitializeDbAsyncDelegate = async(sp, cancellationToken) =>
                        {
                            await defaultBuilder.InitializeDbAsyncDelegate(sp, cancellationToken);

                            await tenantBuilder.InitializeDbAsyncDelegate(sp, cancellationToken);

                            if (tenantConfiguration != null)
                            {
                                await tenantConfiguration.InitializeDbAsync(sp, cancellationToken);
                            }
                        },
                        InitializeAsyncDelegate = async(sp, cancellationToken) =>
                        {
                            await defaultBuilder.InitializeAsyncDelegate(sp, cancellationToken);

                            await tenantBuilder.InitializeAsyncDelegate(sp, cancellationToken);

                            if (tenantConfiguration != null)
                            {
                                await tenantConfiguration.InitializeAsync(sp, cancellationToken);
                            }
                        }
                    };

                    foreach (var hostName in defaultBuilder.HostNames)
                    {
                        _options.HostMappings.Add(hostName, kvp.Key);
                    }

                    foreach (var hostName in tenantBuilder.HostNames)
                    {
                        _options.HostMappings.Add(hostName, kvp.Key);
                    }

                    var tenantConfig = TenantConfig.BuildTenantAppConfiguration(config, hostingEnvironment, kvp.Key, options.ConfigureAppConfigurationDelegate);

                    tenantServices.AddSingleton(tenantConfig);

                    var builderContext = new TenantBuilderContext(kvp.Key)
                    {
                        RootServiceProvider = serviceProvider,
                        Configuration       = tenantConfig,
                        HostingEnvironment  = hostingEnvironment
                    };

                    options.ConfigureServicesDelegate(builderContext, tenantServices);

                    actionBuilder.Add(b => b.Populate(tenantServices));
                    mtc.ConfigureTenant(kvp.Key, actionBuilder.Build());
                }
            }