Skip to content

Commit

Permalink
Register ReCaptchaService as singleton (#14583)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
  • Loading branch information
hishamco and sebastienros authored Oct 26, 2023
1 parent ea9dff3 commit 44a465b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ReCaptchaLoginFilter : IAsyncResultFilter
private readonly ReCaptchaService _reCaptchaService;
private readonly IShapeFactory _shapeFactory;

private ReCaptchaSettings _reCaptchaSettings;

public ReCaptchaLoginFilter(
ILayoutAccessor layoutAccessor,
ISiteService siteService,
Expand All @@ -38,9 +40,9 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
return;
}

var settings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();
_reCaptchaSettings ??= (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();

if (!settings.IsValid())
if (!_reCaptchaSettings.IsValid())
{
await next();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddReCaptcha(this IServiceCollection services, Action<ReCaptchaSettings> 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<ReCaptchaService>()
services.AddSingleton<ReCaptchaService>()
.AddHttpClient(nameof(ReCaptchaService))
.AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, attempt => TimeSpan.FromSeconds(0.5 * attempt)));

Expand Down

0 comments on commit 44a465b

Please sign in to comment.