From 9f96e830fab811863b77935f8bc6bc9a33035ca2 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 19 Dec 2024 15:15:52 +0000 Subject: [PATCH] Fix HttpClient registration for WebhookSender (#1758) The HttpClient configuration wasn't getting picked up as it was registered against the concrete type instead of the interface. This fixes that. --- .../Services/Webhooks/ApplicationBuilderExtensions.cs | 3 +-- .../Services/Webhooks/WebhookSender.cs | 6 ++++-- .../Services/Webhooks/WebhookSenderTests.cs | 6 ++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/ApplicationBuilderExtensions.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/ApplicationBuilderExtensions.cs index 7905b2557..f145c83e4 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/ApplicationBuilderExtensions.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/ApplicationBuilderExtensions.cs @@ -19,8 +19,7 @@ public static IHostApplicationBuilder AddWebhookDeliveryService(this IHostApplic { AddWebhookOptions(builder); - builder.Services.AddSingleton(); - WebhookSender.AddHttpClient(builder.Services); + WebhookSender.Register(builder.Services); builder.Services.AddSingleton(); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/WebhookSender.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/WebhookSender.cs index f60aafc92..48574026f 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/WebhookSender.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Webhooks/WebhookSender.cs @@ -54,7 +54,7 @@ public async Task SendMessageAsync(WebhookMessage message, CancellationToken can response.EnsureSuccessStatusCode(); } - public static void AddHttpClient(IServiceCollection services, Func? getPrimaryHandler = null) + public static void Register(IServiceCollection services, Func? getPrimaryHandler = null) { // We configure the options here manually rather than using the library-provided extension methods so that they don't 'bleed out' globally; // it's feasible we could want a different configuration of, say, AddContentDigestOptions for use elsewhere. @@ -108,8 +108,10 @@ IOptions GetMessageSigningOptions(IServiceProvider servic return new ECDsaP382Sha384SignatureProvider(cert, signingKeyId); }); + services.AddSingleton(); + var httpClientBuilder = services - .AddHttpClient(client => + .AddHttpClient(client => { client.Timeout = TimeSpan.FromSeconds(TimeoutSeconds); client.DefaultRequestHeaders.ExpectContinue = false; diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Tests/Services/Webhooks/WebhookSenderTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Tests/Services/Webhooks/WebhookSenderTests.cs index 02b7513a3..046ae98ca 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Tests/Services/Webhooks/WebhookSenderTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Tests/Services/Webhooks/WebhookSenderTests.cs @@ -134,10 +134,8 @@ public WebhookReceiver() ]; }); - builder.Services.AddSingleton(); - builder.Services.AddSingleton(); - WebhookSender.AddHttpClient(builder.Services, () => _server!.CreateHandler()); + WebhookSender.Register(builder.Services, () => _server!.CreateHandler()); builder.Services.Configure(options => { @@ -206,7 +204,7 @@ public WebhookReceiver() public WebhookMessageRecorder WebhookMessageRecorder => Services.GetRequiredService(); - public WebhookSender GetWebhookSender() => Services.GetRequiredService(); + public IWebhookSender GetWebhookSender() => Services.GetRequiredService(); public WebhookOptions GetWebhookOptions() => Services.GetRequiredService>().Value;