Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register ReCaptchaService as singleton #14583

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading