/// <summary>
        /// Adds the root tenant to the collection, using the <see cref="ITenancyService"/>.
        /// </summary>
        /// <param name="services">The service collection to which to add the root tenant.</param>
        /// <returns>The configured service collection.</returns>
        public static IServiceCollection AddTenantServiceClientRootTenant(this IServiceCollection services)
        {
            if (services.Any(s => typeof(RootTenant).IsAssignableFrom(s.ServiceType)))
            {
                return(services);
            }

            services.AddContent(contentFactory => contentFactory.RegisterTransientContent <Tenant>());

            // Construct a root tenant from the tenant retrieved from the service, using the
            // root tenant ID.
            services.AddSingleton(s =>
            {
                ITenancyService tenancyService         = s.GetRequiredService <ITenancyService>();
                ITenantMapper tenantMapper             = s.GetRequiredService <ITenantMapper>();
                IPropertyBagFactory propertyBagFactory = s.GetRequiredService <IPropertyBagFactory>();
                ITenant fetchedRootTenant = tenantMapper.MapTenant(tenancyService.GetTenant(RootTenant.RootTenantId));
                var localRootTenant       = new RootTenant(propertyBagFactory);
                IReadOnlyDictionary <string, object> propertiesToSetOrAdd = fetchedRootTenant.Properties.AsDictionary();
                localRootTenant.UpdateProperties(propertiesToSetOrAdd);
                return(localRootTenant);
            });

            return(services);
        }
Пример #2
0
        /// <summary>
        /// Adds services an Azure Blob storage-based implementation of <see cref="ITenantProvider"/>.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="getRootTenantStorageConfiguration">
        /// A function that returns the <see cref="BlobStorageConfiguration"/> that will be used for the root tenant to
        /// determine where to store its children.
        /// </param>
        /// <returns>The modified service collection.</returns>
        public static IServiceCollection AddTenantProviderBlobStore(
            this IServiceCollection services,
            Func <IServiceProvider, BlobStorageConfiguration> getRootTenantStorageConfiguration)
        {
            if (services.Any(s => typeof(ITenantProvider).IsAssignableFrom(s.ServiceType)))
            {
                return(services);
            }

            services.AddRequiredTenancyServices();

            services.AddSingleton(sp =>
            {
                BlobStorageConfiguration rootTenantStorageConfig = getRootTenantStorageConfiguration(sp);

                IPropertyBagFactory propertyBagFactory = sp.GetRequiredService <IPropertyBagFactory>();
                var rootTenant = new RootTenant(propertyBagFactory);

                rootTenant.UpdateProperties(
                    values => values.AddBlobStorageConfiguration(
                        TenantProviderBlobStore.ContainerDefinition, rootTenantStorageConfig));

                ITenantCloudBlobContainerFactory tenantCloudBlobContainerFactory = sp.GetRequiredService <ITenantCloudBlobContainerFactory>();
                IJsonSerializerSettingsProvider serializerSettingsProvider       = sp.GetRequiredService <IJsonSerializerSettingsProvider>();

                return(new TenantProviderBlobStore(rootTenant, propertyBagFactory, tenantCloudBlobContainerFactory, serializerSettingsProvider));
            });

            services.AddSingleton <ITenantStore>(sp => sp.GetRequiredService <TenantProviderBlobStore>());
            services.AddSingleton <ITenantProvider>(sp => sp.GetRequiredService <TenantProviderBlobStore>());
            return(services);
        }