Skip to content

Commit

Permalink
Fix HttpClient registration for WebhookSender (#1758)
Browse files Browse the repository at this point in the history
The HttpClient configuration wasn't getting picked up as it was
registered against the concrete type instead of the interface. This
fixes that.
  • Loading branch information
gunndabad authored Dec 19, 2024
1 parent d4bdd23 commit 9f96e83
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static IHostApplicationBuilder AddWebhookDeliveryService(this IHostApplic
{
AddWebhookOptions(builder);

builder.Services.AddSingleton<IWebhookSender, WebhookSender>();
WebhookSender.AddHttpClient(builder.Services);
WebhookSender.Register(builder.Services);

builder.Services.AddSingleton<IHostedService, WebhookDeliveryService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task SendMessageAsync(WebhookMessage message, CancellationToken can
response.EnsureSuccessStatusCode();
}

public static void AddHttpClient(IServiceCollection services, Func<HttpMessageHandler>? getPrimaryHandler = null)
public static void Register(IServiceCollection services, Func<HttpMessageHandler>? 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.
Expand Down Expand Up @@ -108,8 +108,10 @@ IOptions<MessageSigningOptions> GetMessageSigningOptions(IServiceProvider servic
return new ECDsaP382Sha384SignatureProvider(cert, signingKeyId);
});

services.AddSingleton<IWebhookSender, WebhookSender>();

var httpClientBuilder = services
.AddHttpClient<WebhookSender>(client =>
.AddHttpClient<IWebhookSender, WebhookSender>(client =>
{
client.Timeout = TimeSpan.FromSeconds(TimeoutSeconds);
client.DefaultRequestHeaders.ExpectContinue = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ public WebhookReceiver()
];
});

builder.Services.AddSingleton<WebhookSender>();

builder.Services.AddSingleton<WebhookMessageRecorder>();
WebhookSender.AddHttpClient(builder.Services, () => _server!.CreateHandler());
WebhookSender.Register(builder.Services, () => _server!.CreateHandler());

builder.Services.Configure<RequestSignatureVerificationOptions>(options =>
{
Expand Down Expand Up @@ -206,7 +204,7 @@ public WebhookReceiver()

public WebhookMessageRecorder WebhookMessageRecorder => Services.GetRequiredService<WebhookMessageRecorder>();

public WebhookSender GetWebhookSender() => Services.GetRequiredService<WebhookSender>();
public IWebhookSender GetWebhookSender() => Services.GetRequiredService<IWebhookSender>();

public WebhookOptions GetWebhookOptions() => Services.GetRequiredService<IOptions<WebhookOptions>>().Value;

Expand Down

0 comments on commit 9f96e83

Please sign in to comment.