diff --git a/README.md b/README.md index 65279fa..444b373 100644 --- a/README.md +++ b/README.md @@ -465,9 +465,11 @@ Algolia provides [autocomplete](https://www.algolia.com/doc/ui-libraries/autocom 3. Load the Algolia keys from `appsettings.json`: ```cshtml -@inject IConfiguration configuration +@using Microsoft.Extensions.Options +@inject IOptions options + @{ - var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get(); + var algoliaOptions = options.Value; } ``` @@ -1087,9 +1089,11 @@ endpoints.MapControllerRoute( 2. Create the _Index.cshtml_ view for your controller with the basic layout and stylesheet references. Load your Algolia settings for use later: ```cshtml -@inject IConfiguration configuration +@using Microsoft.Extensions.Options +@inject IOptions options + @{ - var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get(); + var algoliaOptions = options.Value; } @section styles { diff --git a/src/AlgoliaStartupExtensions.cs b/src/AlgoliaStartupExtensions.cs index ec2a14b..7c1e068 100644 --- a/src/AlgoliaStartupExtensions.cs +++ b/src/AlgoliaStartupExtensions.cs @@ -1,12 +1,13 @@ -using Algolia.Search.Clients; +using System; + +using Algolia.Search.Clients; using Kentico.Xperience.AlgoliaSearch.Models; using Kentico.Xperience.AlgoliaSearch.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - -using System; +using Microsoft.Extensions.Options; namespace Kentico.Xperience.AlgoliaSearch { @@ -24,23 +25,32 @@ public static class AlgoliaStartupExtensions /// The implementation of to register. public static IServiceCollection AddAlgolia(this IServiceCollection services, IConfiguration configuration, IAlgoliaIndexRegister register) { - var algoliaOptions = configuration.GetSection(AlgoliaOptions.SECTION_NAME).Get(); - if (String.IsNullOrEmpty(algoliaOptions.ApplicationId) || String.IsNullOrEmpty(algoliaOptions.ApiKey)) + services.Configure(configuration.GetSection(AlgoliaOptions.SECTION_NAME)); + services.PostConfigure(options => { - // Algolia configuration is not valid, but IEventLogService can't be resolved during startup. - // Set dummy values so that DI is not broken, but errors can be captured when attempting to use the client - algoliaOptions.ApplicationId = "NO_APP"; - algoliaOptions.ApiKey = "NO_KEY"; - } + if (String.IsNullOrEmpty(options.ApplicationId) || String.IsNullOrEmpty(options.ApiKey)) + { + // Algolia configuration is not valid, but IEventLogService can't be resolved during startup. + // Set dummy values so that DI is not broken, but errors can be captured when attempting to use the client + options.ApplicationId = "NO_APP"; + options.ApiKey = "NO_KEY"; + } + }); - var insightsClient = new InsightsClient(algoliaOptions.ApplicationId, algoliaOptions.ApiKey); - var searchClient = new SearchClient(algoliaOptions.ApplicationId, algoliaOptions.ApiKey); + return services + .AddSingleton(s => + { + var options = s.GetRequiredService>(); - services.AddSingleton(insightsClient); - services.AddSingleton(searchClient); - services.AddSingleton(register); + return new InsightsClient(options.Value.ApplicationId, options.Value.ApiKey); + }) + .AddSingleton(s => + { + var options = s.GetRequiredService>(); - return services; + return new SearchClient(options.Value.ApplicationId, options.Value.ApiKey); + }) + .AddSingleton(register); } } } \ No newline at end of file diff --git a/src/Kentico.Xperience.AlgoliaSearch.csproj b/src/Kentico.Xperience.AlgoliaSearch.csproj index 02f9a31..0b7987b 100644 --- a/src/Kentico.Xperience.AlgoliaSearch.csproj +++ b/src/Kentico.Xperience.AlgoliaSearch.csproj @@ -9,7 +9,7 @@ Xperience Algolia Search Kentico.Xperience.AlgoliaSearch - 3.0.0 + 3.1.0 Kentico Software Kentico Software icon.png