From c15a155d2c6a134905b0f07cea7faf2fd2749e51 Mon Sep 17 00:00:00 2001 From: bkapustik <82807109+bkapustik@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:23:53 +0100 Subject: [PATCH 1/2] Algolia Installer work around to solve problem where Application is not Initialized during Module.OnInit() --- .../Admin/AlgoliaAdminModule.cs | 12 +++++----- .../AlgoliaSearchModule.cs | 4 ---- .../AlgoliaStartupExtensions.cs | 22 +++++++++++++++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs index eb04a77..9481eb9 100644 --- a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs +++ b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs @@ -16,26 +16,24 @@ namespace Kentico.Xperience.Algolia.Admin; internal class AlgoliaAdminModule : AdminModule { private IAlgoliaConfigurationStorageService storageService = null!; - private AlgoliaModuleInstaller installer = null!; + private IServiceProvider serviceProvider = null!; public AlgoliaAdminModule() : base(nameof(AlgoliaAdminModule)) { } protected override void OnInit(ModuleInitParameters parameters) { base.OnInit(parameters); - RegisterClientModule("kentico", "xperience-integrations-algolia"); - var services = parameters.Services; - - installer = services.GetRequiredService(); - storageService = services.GetRequiredService(); + serviceProvider = parameters.Services; + storageService = serviceProvider.GetRequiredService(); - ApplicationEvents.Initialized.Execute += InitializeModule; + ApplicationEvents.PostStart.Execute += InitializeModule; } private void InitializeModule(object? sender, EventArgs e) { + var installer = serviceProvider.GetRequiredService(); installer.Install(); AlgoliaIndexStore.SetIndicies(storageService); diff --git a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs index 3c8dbe4..042380e 100644 --- a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs +++ b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs @@ -34,15 +34,11 @@ public AlgoliaSearchModule() : base(nameof(AlgoliaSearchModule)) protected override void OnInit(ModuleInitParameters parameters) { base.OnInit(); - var services = parameters.Services; - services.GetRequiredService().Install(); algoliaTaskLogger = Service.Resolve(); appSettingsService = Service.Resolve(); conversionService = Service.Resolve(); - - AddRegisteredIndices(); WebPageEvents.Publish.Execute += HandleEvent; WebPageEvents.Delete.Execute += HandleEvent; ContentItemEvents.Publish.Execute += HandleContentItemEvent; diff --git a/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs b/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs index 974c355..678dde7 100644 --- a/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs +++ b/src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs @@ -18,7 +18,7 @@ public static class AlgoliaStartupExtensions /// /// The service collection. /// The application configuration. - public static IServiceCollection AddKenticoAlgolia(this IServiceCollection services, IConfiguration configuration) => + private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollection services, IConfiguration configuration) => services.AddSingleton() .Configure(configuration.GetSection(AlgoliaOptions.CMS_ALGOLIA_SECTION_NAME)) .AddSingleton(s => @@ -53,7 +53,7 @@ public static IServiceCollection AddKenticoAlgolia(this IServiceCollection servi /// public static IServiceCollection AddKenticoAlgolia(this IServiceCollection serviceCollection, Action configure, IConfiguration configuration) { - serviceCollection.AddKenticoAlgolia(configuration); + serviceCollection.AddKenticoAlgoliaInternal(configuration); var builder = new AlgoliaBuilder(serviceCollection); @@ -67,6 +67,24 @@ public static IServiceCollection AddKenticoAlgolia(this IServiceCollection servi return serviceCollection; } + + /// + /// Adds Algolia services and custom module to application with + /// + /// + /// The application configuration. + /// + public static IServiceCollection AddKenticoAlgolia(this IServiceCollection serviceCollection, IConfiguration configuration) + { + serviceCollection.AddKenticoAlgoliaInternal(configuration); + + var builder = new AlgoliaBuilder(serviceCollection); + + serviceCollection.AddTransient(); + builder.RegisterStrategy("Default"); + + return serviceCollection; + } } From 9329d822739b6892ba1e08802923fcf13b6cd962 Mon Sep 17 00:00:00 2001 From: bkapustik <82807109+bkapustik@users.noreply.github.com> Date: Mon, 12 Feb 2024 13:00:42 +0100 Subject: [PATCH 2/2] Algolia Module Installer updated to be according to Integration standard --- .../Admin/AlgoliaAdminModule.cs | 9 +++++---- src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs | 9 ++++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs index 9481eb9..c2a16d0 100644 --- a/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs +++ b/src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs @@ -16,7 +16,7 @@ namespace Kentico.Xperience.Algolia.Admin; internal class AlgoliaAdminModule : AdminModule { private IAlgoliaConfigurationStorageService storageService = null!; - private IServiceProvider serviceProvider = null!; + private AlgoliaModuleInstaller installer = null!; public AlgoliaAdminModule() : base(nameof(AlgoliaAdminModule)) { } @@ -25,15 +25,16 @@ protected override void OnInit(ModuleInitParameters parameters) base.OnInit(parameters); RegisterClientModule("kentico", "xperience-integrations-algolia"); - serviceProvider = parameters.Services; - storageService = serviceProvider.GetRequiredService(); + var services = parameters.Services; + + installer = services.GetRequiredService(); + storageService = services.GetRequiredService(); ApplicationEvents.PostStart.Execute += InitializeModule; } private void InitializeModule(object? sender, EventArgs e) { - var installer = serviceProvider.GetRequiredService(); installer.Install(); AlgoliaIndexStore.SetIndicies(storageService); diff --git a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs index 042380e..15c9ea9 100644 --- a/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs +++ b/src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs @@ -35,9 +35,12 @@ protected override void OnInit(ModuleInitParameters parameters) { base.OnInit(); - algoliaTaskLogger = Service.Resolve(); - appSettingsService = Service.Resolve(); - conversionService = Service.Resolve(); + var services = parameters.Services; + + algoliaTaskLogger = services.GetRequiredService(); + appSettingsService = services.GetRequiredService(); + conversionService = services.GetRequiredService(); + WebPageEvents.Publish.Execute += HandleEvent; WebPageEvents.Delete.Execute += HandleEvent;