示例#1
0
        private MultitenancyOptions <TTenant> CreateOptions(IConfigurationRoot config, string name)
        {
            IConfigurationSection         multitenantConfiguration = config.GetSection(nameof(MultitenancyConstants.MultitenancyOptions));
            MultitenancyOptions <TTenant> options = multitenantConfiguration.Get <MultitenancyOptions <TTenant> >();

            lock (SyncLock)
            {
                if (IsPostConfigureNeeded)
                {
                    IsPostConfigureNeeded = false;

                    if (options != null)
                    {
                        options.TenantsConfigurations = multitenantConfiguration.GetSection(MultitenancyConstants.Tenants)?.GetChildren();
                        foreach (IConfigureOptions <MultitenancyOptions <TTenant> > setup in Setups)
                        {
                            if (setup is IConfigureNamedOptions <MultitenancyOptions <TTenant> > namedSetup)
                            {
                                namedSetup.Configure(name, options);
                            }
                            else if (name == Options.DefaultName)
                            {
                                setup.Configure(options);
                            }
                        }
                        foreach (IPostConfigureOptions <MultitenancyOptions <TTenant> > post in PostConfigures)
                        {
                            post.PostConfigure(name, options);
                        }
                    }
                }
            }

            return(options);
        }
示例#2
0
            public ConfigureAzureOptions(IOptions <AzureAdOptions> azureOptions, IOptions <MultitenancyOptions> multitenancyOptions,
                                         IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider)

            {
                _azureOptions        = azureOptions.Value;
                _multitenancyOptions = multitenancyOptions.Value;
                _serviceProvider     = serviceProvider;
                _baseUrl             = httpContextAccessor.HttpContext.Request.GetDisplayUrl();
            }
示例#3
0
        /// <summary>
        /// Add multitenancy feature.
        /// </summary>
        /// <typeparam name="TStartup">The stratup class.</typeparam>
        /// <typeparam name="TTenant">The tenant class.</typeparam>
        /// <typeparam name="TResolver">The Resolver tenant.</typeparam>
        /// <param name="hostBuilder">hostBuilder.</param>
        /// <param name="startupType">The type of the startup class.</param>
        /// <param name="multitenancyConfiguration">The configuration which contains MultitenancyOptions.</param>
        /// <param name="throwErrorIfOptionsNotFound">Boolean to indicate if throws exception when MultitenancyOptions is not found.</param>
        /// <param name="actionConfiguration">Additionnal config.</param>
        /// <returns>IWebHostBuilder.</returns>
        internal static IWebHostBuilder UseUnobtrusiveMulitenancyStartup <TStartup, TTenant, TResolver>(
            this IWebHostBuilder hostBuilder,
            Type startupType,
            IConfigurationRoot multitenancyConfiguration,
            bool throwErrorIfOptionsNotFound,
            Action <MultiTenancyConfig <TTenant> > actionConfiguration)
            where TStartup : class
            where TTenant : class
            where TResolver : class, ITenantResolver <TTenant>
        {
            if (hostBuilder == null)
            {
                throw new ArgumentNullException(nameof(hostBuilder));
            }

            string env = hostBuilder.GetSetting("environment");

            IPostConfigureOptions <MultitenancyOptions <TTenant> >[] postConfigures = new[] {
                new MultitenancyPostConfigureOptions <TTenant>()
            };
            MultiTenancyConfig <TTenant> multitenancyConfig = new MultiTenancyConfig <TTenant>(
                env,
                multitenancyConfiguration,
                postConfigures: postConfigures);
            MultitenancyOptions <TTenant> buildedOptions = multitenancyConfig.CurrentMultiTenacyOptionsValue;

            if (throwErrorIfOptionsNotFound && (buildedOptions == null || !(buildedOptions?.Tenants?.Any() ?? false)))
            {
                throw new Exception("MultitenancyOptions not found in configuration.");
            }

            if (!(buildedOptions?.Tenants?.Any() ?? false))
            {
                return(hostBuilder.UseStartup <TStartup>());
            }
            else
            {
                string startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name;

                return(hostBuilder
                       .UseDefaultServiceProvider(options => options.ValidateScopes = false)
                       .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);
                    hostBuilder.UseSetting(MultitenancyConstants.UseUnobstrusiveMulitenancyStartupKey, startupAssemblyName);
                    hostBuilder.UseSetting(WebHostDefaults.StartupAssemblyKey, null);
                })
                       .ConfigureServices((WebHostBuilderContext ctx, IServiceCollection services) =>
                {
                    services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
                    //services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();

                    // Register multitenancy options.
                    services.AddMultitenancyOptions <TTenant>(multitenancyConfig);
                    services.AddMultitenancy <TTenant, TResolver>();

                    // manage IStartup
                    services.RemoveAll <IStartup>();

                    // Override config.
                    // Add logging
                    multitenancyConfig.UseColoredConsoleLogProvider();
                    actionConfiguration?.Invoke(multitenancyConfig);
                    services.TryAdd(ServiceDescriptor.Singleton <ILogProvider>(LogProvider.CurrentLogProvider));
                    services.TryAdd(ServiceDescriptor.Singleton(typeof(ILog <>), typeof(Log <>)));
                    services.AddSingleton <MultiTenancyConfig <TTenant> >(serviceProvider => multitenancyConfig);

                    ServiceDescriptor descriptor = BuildMultitenantRequestStartupFilter <TStartup, TTenant>();
                    services.Insert(0, descriptor);

                    ServiceDescriptor startuptDescriptor = BuildConventionMultitenantBasedStartup(startupType, multitenancyConfig);
                    services.Insert(1, startuptDescriptor);
                })
                       );
            }
        }