From 3cc1e213a9b82cb660f656cced35c1c90e056acc Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Thu, 19 Oct 2023 13:23:15 -0700 Subject: [PATCH 1/3] Change how ReCacptchaService consume HttpClient --- .../ServiceCollectionExtensions.cs | 4 ++-- .../Services/ReCaptchaService.cs | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs b/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs index 9b750f5cc63..95eabaded17 100644 --- a/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs +++ b/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs @@ -14,8 +14,8 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddReCaptcha(this IServiceCollection services, Action configure = null) { // c.f. https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests - services.AddScoped() - .AddHttpClient() + services.AddTransient() + .AddHttpClient(nameof(ReCaptchaService)) .AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(0.5 * attempt))); services.AddSingleton(); diff --git a/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs b/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs index d73d10c5a07..c263c8e294b 100644 --- a/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs +++ b/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs @@ -23,22 +23,22 @@ public class ReCaptchaService }; private readonly ReCaptchaSettings _reCaptchaSettings; - private readonly HttpClient _httpClient; + private readonly IHttpClientFactory _httpClientFactory; private readonly IEnumerable _robotDetectors; private readonly IHttpContextAccessor _httpContextAccessor; private readonly ILogger _logger; protected readonly IStringLocalizer S; public ReCaptchaService( - HttpClient httpClient, + IHttpClientFactory httpClientFactory, IOptions optionsAccessor, IEnumerable robotDetectors, IHttpContextAccessor httpContextAccessor, ILogger logger, IStringLocalizer stringLocalizer) { + _httpClientFactory = httpClientFactory; _reCaptchaSettings = optionsAccessor.Value; - _httpClient = httpClient; _robotDetectors = robotDetectors; _httpContextAccessor = httpContextAccessor; _logger = logger; @@ -123,7 +123,8 @@ private async Task VerifyAsync(string responseToken) { "response", responseToken } }); - var response = await _httpClient.PostAsync($"{_reCaptchaSettings.ReCaptchaApiUri.TrimEnd('/')}/siteverify", content); + var httpClient = _httpClientFactory.CreateClient(nameof(ReCaptchaService)); + var response = await httpClient.PostAsync($"{_reCaptchaSettings.ReCaptchaApiUri.TrimEnd('/')}/siteverify", content); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadFromJsonAsync(_jsonSerializerOptions); From e7ae26a47453692e2e6255827f1860a8264df2ce Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Thu, 19 Oct 2023 14:11:55 -0700 Subject: [PATCH 2/3] make HttpClient instance singleton --- .../OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs b/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs index c263c8e294b..737c7cf0dc9 100644 --- a/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs +++ b/src/OrchardCore/OrchardCore.ReCaptcha.Core/Services/ReCaptchaService.cs @@ -28,6 +28,7 @@ public class ReCaptchaService private readonly IHttpContextAccessor _httpContextAccessor; private readonly ILogger _logger; protected readonly IStringLocalizer S; + private HttpClient _httpClient; public ReCaptchaService( IHttpClientFactory httpClientFactory, @@ -123,8 +124,8 @@ private async Task VerifyAsync(string responseToken) { "response", responseToken } }); - var httpClient = _httpClientFactory.CreateClient(nameof(ReCaptchaService)); - var response = await httpClient.PostAsync($"{_reCaptchaSettings.ReCaptchaApiUri.TrimEnd('/')}/siteverify", content); + _httpClient ??= _httpClientFactory.CreateClient(nameof(ReCaptchaService)); + var response = await _httpClient.PostAsync($"{_reCaptchaSettings.ReCaptchaApiUri.TrimEnd('/')}/siteverify", content); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadFromJsonAsync(_jsonSerializerOptions); From c7695418f8c943144d3dfff6e49a462926545b35 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Thu, 19 Oct 2023 14:31:53 -0700 Subject: [PATCH 3/3] Register `ReCaptchaService` as scoped --- .../OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs b/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs index 95eabaded17..16444224da6 100644 --- a/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs +++ b/src/OrchardCore/OrchardCore.ReCaptcha.Core/ServiceCollectionExtensions.cs @@ -14,7 +14,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddReCaptcha(this IServiceCollection services, Action configure = null) { // c.f. https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests - services.AddTransient() + services.AddScoped() .AddHttpClient(nameof(ReCaptchaService)) .AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(0.5 * attempt)));