diff --git a/src/Geta.NotFoundHandler/Core/ScheduledJobs/ServiceCollectionExtensions.cs b/src/Geta.NotFoundHandler/Core/ScheduledJobs/ServiceCollectionExtensions.cs index a698cf7..2c267e3 100644 --- a/src/Geta.NotFoundHandler/Core/ScheduledJobs/ServiceCollectionExtensions.cs +++ b/src/Geta.NotFoundHandler/Core/ScheduledJobs/ServiceCollectionExtensions.cs @@ -1,28 +1,38 @@ // Copyright (c) Geta Digital. All rights reserved. // Licensed under Apache-2.0. See the LICENSE file in the project root for more information +using System; using Coravel; using Geta.NotFoundHandler.Core.ScheduledJobs.Suggestions; using Geta.NotFoundHandler.Infrastructure.Configuration; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; namespace Geta.NotFoundHandler.Core.ScheduledJobs; public static class ServiceCollectionExtensions { - public static IServiceCollection EnableScheduler(this IServiceCollection services) + [Obsolete("Use EnableScheduler with IConfigurationSection parameter instead.")] + public static IServiceCollection EnableScheduler( + this IServiceCollection services) { - using var serviceProvider = services.BuildServiceProvider(); - var options = serviceProvider.GetRequiredService>().Value; + return services.EnableScheduler(null); + } + + public static IServiceCollection EnableScheduler( + this IServiceCollection services, + IConfigurationSection notFoundHandlerConfiguration) + { + var options = notFoundHandlerConfiguration?.Get(); - if (options.UseInternalScheduler) + if (!(options?.UseInternalScheduler ?? false)) { - services.AddScheduler(); - - services.AddTransient(); + return services; } + services.AddScheduler(); + services.AddTransient(); + return services; } } diff --git a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ConfigureNotFoundHandlerOptions.cs b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ConfigureNotFoundHandlerOptions.cs new file mode 100644 index 0000000..2bd6d18 --- /dev/null +++ b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ConfigureNotFoundHandlerOptions.cs @@ -0,0 +1,19 @@ +using System; +using Microsoft.Extensions.Options; + +namespace Geta.NotFoundHandler.Infrastructure.Configuration; + +public class ConfigureNotFoundHandlerOptions : IConfigureOptions +{ + private readonly Action _setupAction; + + public ConfigureNotFoundHandlerOptions(Action setupAction) + { + _setupAction = setupAction; + } + + public void Configure(NotFoundHandlerOptions options) + { + _setupAction?.Invoke(options); + } +} diff --git a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs index 02a79a4..8b6fdf4 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs @@ -20,17 +20,45 @@ public static class ServiceCollectionExtensions { private static readonly Action DefaultPolicy = p => p.RequireRole("Administrators"); + [Obsolete("Use AddNotFoundHandler(Action, IConfiguration) instead.")] public static IServiceCollection AddNotFoundHandler( this IServiceCollection services, Action setupAction) { - return AddNotFoundHandler(services, setupAction, DefaultPolicy); + return services.AddNotFoundHandler(setupAction, DefaultPolicy); } + public static IServiceCollection AddNotFoundHandler( + this IServiceCollection services, + Action setupAction, + IConfiguration configuration) + { + return services.AddNotFoundHandlerInternal(setupAction, DefaultPolicy, configuration); + } + + [Obsolete("Use AddNotFoundHandler(Action, Action, IConfiguration) instead.")] public static IServiceCollection AddNotFoundHandler( this IServiceCollection services, Action setupAction, Action configurePolicy) + { + return AddNotFoundHandlerInternal(services, setupAction, configurePolicy); + } + + public static IServiceCollection AddNotFoundHandler( + this IServiceCollection services, + Action setupAction, + Action configurePolicy, + IConfiguration configuration) + { + return AddNotFoundHandlerInternal(services, setupAction, configurePolicy, configuration); + } + + private static IServiceCollection AddNotFoundHandlerInternal( + this IServiceCollection services, + Action setupAction, + Action configurePolicy, + IConfiguration configuration = null) { services.AddTransient(); @@ -80,6 +108,19 @@ public static IServiceCollection AddNotFoundHandler( x.GetRequiredService())); services.AddTransient(); + IConfigurationSection notFoundHandlerConfiguration = null; + + if (configuration != null) + { + notFoundHandlerConfiguration = configuration.GetSection(NotFoundHandlerOptions.Section); + + services.Configure(notFoundHandlerConfiguration); + } + + services + .AddTransient(_ => setupAction) + .ConfigureOptions(); + var providerOptions = new NotFoundHandlerOptions(); setupAction(providerOptions); foreach (var provider in providerOptions.Providers) @@ -87,12 +128,6 @@ public static IServiceCollection AddNotFoundHandler( services.AddTransient(typeof(INotFoundHandler), provider); } - services.AddOptions().Configure((options, configuration) => - { - setupAction(options); - configuration.GetSection(NotFoundHandlerOptions.Section).Bind(options); - }); - services.AddAuthorization(options => { options.AddPolicy(Constants.PolicyName, configurePolicy); @@ -100,7 +135,7 @@ public static IServiceCollection AddNotFoundHandler( services.AddSingleton(); - services.EnableScheduler(); + services.EnableScheduler(notFoundHandlerConfiguration); return services; }