public static IUnleashServiceCollection WithMemoryToggleCollectionCache(this IUnleashServiceCollection serviceCollection, Action <MemoryToggleCollectionCacheSettings> settingsConfigurator = null)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            var settings = new MemoryToggleCollectionCacheSettings();

            if (serviceCollection.UnleashConfiguration != null)
            {
                var section = serviceCollection.UnleashConfiguration.GetSection("Caching:Memory");
                section.Bind(settings);
            }

            settingsConfigurator?.Invoke(settings);

            SettingsValidator.Validate(settings);

            serviceCollection.AddSingleton(settings);

            serviceCollection.AddMemoryCache();
            serviceCollection.WithToggleCollectionCache <MemoryToggleCollectionCache>();

            return(serviceCollection);
        }
예제 #2
0
        public static IUnleashServiceCollection WithNewtonsoftJsonSerializer(this IUnleashServiceCollection serviceCollection,
                                                                             Action <NewtonsoftJsonSerializerSettings> settingsConfigurator = null)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            var settings = new NewtonsoftJsonSerializerSettings();

            if (serviceCollection.UnleashConfiguration != null)
            {
                var section = serviceCollection.UnleashConfiguration.GetSection("Serialization:NewtonsoftJson");
                section.Bind(settings);
            }

            settingsConfigurator?.Invoke(settings);

            SettingsValidator.Validate(settings);

            serviceCollection.AddSingleton(settings);

            serviceCollection.WithJsonSerializer <NewtonsoftJsonSerializer>();

            return(serviceCollection);
        }
        public static IUnleashServiceCollection WithAdminHttpClientFactory(this IUnleashServiceCollection serviceCollection, Action <IHttpClientBuilder> httpClientConfigurator = null)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            var httpClientBuilder = serviceCollection.AddHttpClient <IUnleashAdminApiClient, UnleashAdminApiClient>()
                                    .ConfigureHttpClient(
                (sp, httpClient) =>
            {
                var settings = sp.GetRequiredService <UnleashSettings>();

                httpClient.BaseAddress = settings.UnleashApi;
                httpClient.DefaultRequestHeaders.ConnectionClose = false;

                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

                httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
                {
                    NoCache = true
                };
            });

            serviceCollection.AddSingleton <IUnleashApiClientFactory, HttpClientFactoryApiClientFactory>();

            httpClientConfigurator?.Invoke(httpClientBuilder);
            return(serviceCollection);
        }
예제 #4
0
        public static IUnleashServiceCollection WithWebHostControlledLifetime(this IUnleashServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddTransient <IStartupFilter, WebHostControlledLifetimeStartupFilter>();
            return(serviceCollection);
        }
예제 #5
0
        public static IUnleashServiceCollection WithStrategy <TStrategy>(this IUnleashServiceCollection serviceCollection, Func <IServiceProvider, TStrategy> strategyFactory)
            where TStrategy : class, IStrategy
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddSingleton <IStrategy>(strategyFactory);
            return(serviceCollection);
        }
예제 #6
0
        public static IUnleashServiceCollection WithJsonSerializer <TJsonSerializer>(this IUnleashServiceCollection serviceCollection, Func <IServiceProvider, TJsonSerializer> jsonSerializerFactory)
            where TJsonSerializer : class, IJsonSerializer
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddSingleton <IJsonSerializer>(jsonSerializerFactory);
            return(serviceCollection);
        }
예제 #7
0
        public static IUnleashServiceCollection WithHostControlledLifetime(this IUnleashServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddSingleton <IHostedService, HostControlledLifetimeService>();

            return(serviceCollection);
        }
예제 #8
0
        public static IUnleashServiceCollection WithDefaultStrategies(this IUnleashServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            return(serviceCollection
                   .WithStrategy <DefaultStrategy>()
                   .WithStrategy <UserWithIdStrategy>()
                   .WithStrategy <GradualRolloutUserIdStrategy>()
                   .WithStrategy <GradualRolloutRandomStrategy>()
                   .WithStrategy <ApplicationHostnameStrategy>()
                   .WithStrategy <GradualRolloutSessionIdStrategy>()
                   .WithStrategy <RemoteAddressStrategy>());
        }
예제 #9
0
        public static IUnleashServiceCollection WithSynchronousFlagLoadingOnStartup(
            this IUnleashServiceCollection serviceCollection,
            bool onlyOnEmptyCache = false,
            TimeSpan?timeout      = null)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.Configure <SynchronousFlagLoadingServiceOptions>(opt =>
            {
                opt.OnlyOnEmptyCache = onlyOnEmptyCache;
                opt.Timeout          = timeout ?? DefaultTimeout;
            });

            serviceCollection.AddHostedService <SynchronousFlagLoadingService>();

            return(serviceCollection);
        }
예제 #10
0
        public static IUnleashServiceCollection WithSynchronousFlagLoadingOnStartup(this IUnleashServiceCollection serviceCollection, bool enabled = true, bool onlyOnEmptyCache = false, TimeSpan?timeout = null)
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            if (enabled)
            {
                serviceCollection.AddTransient <IStartupFilter>(
                    sp =>
                {
                    var services = sp.GetRequiredService <IUnleashServices>();
                    return(new SynchronousFlagLoadingStartupFilter(
                               sp, services, onlyOnEmptyCache, timeout ?? DefaultTimeout));
                });
            }

            return(serviceCollection);
        }
예제 #11
0
        public static IUnleashServiceCollection WithScheduledTaskManager <TScheduledTaskManager>(this IUnleashServiceCollection serviceCollection, Func <IServiceProvider, TScheduledTaskManager> scheduledTaskManagerFactory)
            where TScheduledTaskManager : class, IUnleashScheduledTaskManager
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddSingleton <IUnleashScheduledTaskManager>(scheduledTaskManagerFactory);
            return(serviceCollection);
        }
예제 #12
0
        public static IUnleashServiceCollection WithToggleCollectionCache <TToggleCollectionCache>(this IUnleashServiceCollection serviceCollection, Func <IServiceProvider, TToggleCollectionCache> toggleCollectionCacheFactory)
            where TToggleCollectionCache : class, IToggleCollectionCache
        {
            if (serviceCollection == null)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddSingleton <IToggleCollectionCache, TToggleCollectionCache>(toggleCollectionCacheFactory);
            return(serviceCollection);
        }