diff --git a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Startup.cs b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Startup.cs index 9f1a6b79d20..5a3ad95badf 100644 --- a/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Startup.cs +++ b/src/OrchardCore.Modules/OrchardCore.Search.Elasticsearch/Startup.cs @@ -20,7 +20,6 @@ using OrchardCore.Deployment; using OrchardCore.DisplayManagement.Descriptors; using OrchardCore.DisplayManagement.Handlers; -using OrchardCore.Environment.Shell.Builders; using OrchardCore.Environment.Shell.Configuration; using OrchardCore.Modules; using OrchardCore.Mvc.Core.Utilities; @@ -61,138 +60,96 @@ public override void ConfigureServices(IServiceCollection services) var configuration = _shellConfiguration.GetSection(ConfigSectionName); var elasticConfiguration = configuration.Get(); - if (CheckOptions(elasticConfiguration, _logger)) + if (!CheckOptions(elasticConfiguration, _logger)) { - services.Configure(o => o.ConfigurationExists = true); - - IConnectionPool pool = null; - var uris = elasticConfiguration.Ports.Select(port => new Uri($"{elasticConfiguration.Url}:{port}")).Distinct(); - - switch (elasticConfiguration.ConnectionType) - { - case "SingleNodeConnectionPool": - pool = new SingleNodeConnectionPool(uris.First()); - break; - - case "CloudConnectionPool": - BasicAuthenticationCredentials credentials = null; - - if (!string.IsNullOrWhiteSpace(elasticConfiguration.Username) && !string.IsNullOrWhiteSpace(elasticConfiguration.Password) && !string.IsNullOrWhiteSpace(elasticConfiguration.CloudId)) - { - credentials = new BasicAuthenticationCredentials(elasticConfiguration.Username, elasticConfiguration.Password); - pool = new CloudConnectionPool(elasticConfiguration.CloudId, credentials); - } - break; - - case "StaticConnectionPool": - pool = new StaticConnectionPool(uris); - break; - - case "SniffingConnectionPool": - pool = new SniffingConnectionPool(uris); - break; - - case "StickyConnectionPool": - pool = new StickyConnectionPool(uris); - break; + return; + } - default: - pool = new SingleNodeConnectionPool(uris.First()); - break; - } + services.Configure(o => o.ConfigurationExists = true); + var settings = GetConnectionSettings(elasticConfiguration); - var settings = new ConnectionSettings(pool).ThrowExceptions(); + services.AddSingleton(new ElasticClient(settings)); - if (elasticConfiguration.ConnectionType != "CloudConnectionPool" && !string.IsNullOrWhiteSpace(elasticConfiguration.Username) && !string.IsNullOrWhiteSpace(elasticConfiguration.Password)) - { - settings.BasicAuthentication(elasticConfiguration.Username, elasticConfiguration.Password); - } + services.Configure(o => + { + o.IndexPrefix = configuration.GetValue(nameof(o.IndexPrefix)); - if (!string.IsNullOrWhiteSpace(elasticConfiguration.CertificateFingerprint)) - { - settings.CertificateFingerprint(elasticConfiguration.CertificateFingerprint); - } + var jsonNode = configuration.GetSection(nameof(o.Analyzers)).AsJsonNode(); + var jsonElement = JsonSerializer.Deserialize(jsonNode); - if (elasticConfiguration.EnableApiVersioningHeader) + var analyzersObject = JsonObject.Create(jsonElement, new JsonNodeOptions() { - settings.EnableApiVersioningHeader(); - } + PropertyNameCaseInsensitive = true, + }); - var client = new ElasticClient(settings); - services.AddSingleton(client); - services.Configure(o => + if (analyzersObject != null) { - o.IndexPrefix = configuration.GetValue(nameof(o.IndexPrefix)); - - var jsonNode = configuration.GetSection(nameof(o.Analyzers)).AsJsonNode(); - var jsonElement = JsonSerializer.Deserialize(jsonNode); - - var analyzersObject = JsonObject.Create(jsonElement, new JsonNodeOptions() + foreach (var analyzer in analyzersObject) { - PropertyNameCaseInsensitive = true, - }); - - if (analyzersObject != null) - { - foreach (var analyzer in analyzersObject) + if (analyzer.Value == null) { - if (analyzer.Value == null) - { - continue; - } - - o.Analyzers.Add(analyzer.Key, analyzer.Value.AsObject()); + continue; } - } - if (o.Analyzers.Count == 0) - { - // When no analyzers are configured, we'll define a default analyzer. - o.Analyzers.Add(ElasticsearchConstants.DefaultAnalyzer, new JsonObject - { - ["type"] = "standard", - }); + o.Analyzers.Add(analyzer.Key, analyzer.Value.AsObject()); } - }); + } - try + if (o.Analyzers.Count == 0) { - var response = client.Ping(); - - services.Configure(o => + // When no analyzers are configured, we'll define a default analyzer. + o.Analyzers.Add(ElasticsearchConstants.DefaultAnalyzer, new JsonObject { - o.MemberAccessStrategy.Register(); - o.MemberAccessStrategy.Register(); - o.MemberAccessStrategy.Register(); + ["type"] = "standard", }); - - services.AddElasticServices(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped, ElasticSettingsDisplayDriver>(); - services.AddScoped, ElasticQueryDisplayDriver>(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(sp => sp.GetRequiredService()); - services.AddScoped(); } - catch (Exception ex) - { - _logger.LogError(ex, "Elasticsearch is enabled but not active because the connection failed."); - } - } + }); + + services.Configure(o => + { + o.MemberAccessStrategy.Register(); + o.MemberAccessStrategy.Register(); + o.MemberAccessStrategy.Register(); + }); + + services.AddElasticServices(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped, ElasticSettingsDisplayDriver>(); + services.AddScoped, ElasticQueryDisplayDriver>(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(sp => sp.GetRequiredService()); + services.AddScoped(); } - public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) + private static ConnectionSettings GetConnectionSettings(ElasticConnectionOptions elasticConfiguration) { - var options = serviceProvider.GetRequiredService>().Value; + var pool = GetConnectionPool(elasticConfiguration); + + var settings = new ConnectionSettings(pool); - if (!options.ConfigurationExists) + if (elasticConfiguration.ConnectionType != "CloudConnectionPool" && !string.IsNullOrWhiteSpace(elasticConfiguration.Username) && !string.IsNullOrWhiteSpace(elasticConfiguration.Password)) { - return; + settings.BasicAuthentication(elasticConfiguration.Username, elasticConfiguration.Password); + } + + if (!string.IsNullOrWhiteSpace(elasticConfiguration.CertificateFingerprint)) + { + settings.CertificateFingerprint(elasticConfiguration.CertificateFingerprint); + } + + if (elasticConfiguration.EnableApiVersioningHeader) + { + settings.EnableApiVersioningHeader(); } + return settings; + } + + public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) + { var adminControllerName = typeof(AdminController).ControllerName(); routes.MapAreaControllerRoute( @@ -240,14 +197,14 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro private static bool CheckOptions(ElasticConnectionOptions elasticConnectionOptions, ILogger logger) { - var optionsAreValid = true; - if (elasticConnectionOptions == null) { logger.LogError("Elasticsearch is enabled but not active because the configuration is missing."); return false; } + var optionsAreValid = true; + if (string.IsNullOrWhiteSpace(elasticConnectionOptions.Url)) { logger.LogError("Elasticsearch is enabled but not active because the 'Url' is missing or empty in application configuration."); @@ -262,6 +219,44 @@ private static bool CheckOptions(ElasticConnectionOptions elasticConnectionOptio return optionsAreValid; } + + private static IConnectionPool GetConnectionPool(ElasticConnectionOptions elasticConfiguration) + { + var uris = elasticConfiguration.Ports.Select(port => new Uri($"{elasticConfiguration.Url}:{port}")).Distinct(); + IConnectionPool pool = null; + switch (elasticConfiguration.ConnectionType) + { + case "SingleNodeConnectionPool": + pool = new SingleNodeConnectionPool(uris.First()); + break; + + case "CloudConnectionPool": + if (!string.IsNullOrWhiteSpace(elasticConfiguration.Username) && !string.IsNullOrWhiteSpace(elasticConfiguration.Password) && !string.IsNullOrWhiteSpace(elasticConfiguration.CloudId)) + { + var credentials = new BasicAuthenticationCredentials(elasticConfiguration.Username, elasticConfiguration.Password); + pool = new CloudConnectionPool(elasticConfiguration.CloudId, credentials); + } + break; + + case "StaticConnectionPool": + pool = new StaticConnectionPool(uris); + break; + + case "SniffingConnectionPool": + pool = new SniffingConnectionPool(uris); + break; + + case "StickyConnectionPool": + pool = new StickyConnectionPool(uris); + break; + + default: + pool = new SingleNodeConnectionPool(uris.First()); + break; + } + + return pool; + } } [RequireFeatures("OrchardCore.Deployment")] @@ -269,24 +264,21 @@ public class DeploymentStartup : StartupBase { public override void ConfigureServices(IServiceCollection services) { - if (services.Any(d => d.GetImplementationType() == typeof(ElasticsearchService))) - { - services.AddTransient(); - services.AddSingleton(new DeploymentStepFactory()); - services.AddScoped, ElasticIndexDeploymentStepDriver>(); + services.AddTransient(); + services.AddSingleton(new DeploymentStepFactory()); + services.AddScoped, ElasticIndexDeploymentStepDriver>(); - services.AddTransient(); - services.AddSingleton(new DeploymentStepFactory()); - services.AddScoped, ElasticSettingsDeploymentStepDriver>(); + services.AddTransient(); + services.AddSingleton(new DeploymentStepFactory()); + services.AddScoped, ElasticSettingsDeploymentStepDriver>(); - services.AddTransient(); - services.AddSingleton(new DeploymentStepFactory()); - services.AddScoped, ElasticIndexRebuildDeploymentStepDriver>(); + services.AddTransient(); + services.AddSingleton(new DeploymentStepFactory()); + services.AddScoped, ElasticIndexRebuildDeploymentStepDriver>(); - services.AddTransient(); - services.AddSingleton(new DeploymentStepFactory()); - services.AddScoped, ElasticIndexResetDeploymentStepDriver>(); - } + services.AddTransient(); + services.AddSingleton(new DeploymentStepFactory()); + services.AddScoped, ElasticIndexResetDeploymentStepDriver>(); } } @@ -295,10 +287,7 @@ public class ElasticWorkerStartup : StartupBase { public override void ConfigureServices(IServiceCollection services) { - if (services.Any(d => d.GetImplementationType() == typeof(ElasticsearchService))) - { - services.AddSingleton(); - } + services.AddSingleton(); } } @@ -307,12 +296,9 @@ public class ElasticContentPickerStartup : StartupBase { public override void ConfigureServices(IServiceCollection services) { - if (services.Any(d => d.GetImplementationType() == typeof(ElasticsearchService))) - { - services.AddScoped(); - services.AddScoped(); - services.AddShapeAttributes(); - } + services.AddScoped(); + services.AddScoped(); + services.AddShapeAttributes(); } } }