Skip to content

Commit

Permalink
Register ReCaptchaService as singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Oct 26, 2023
1 parent 7817e22 commit 88ad139
Show file tree
Hide file tree
Showing 2 changed files with 8 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,12 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
return;
}

var settings = (await _siteService.GetSiteSettingsAsync()).As<ReCaptchaSettings>();
if (_reCaptchaSettings == null)
{
_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 88ad139

Please sign in to comment.