Skip to content

Commit 9ba93cb

Browse files
[release/9.4] Fix GitHubModels health check dependency on IHttpClientFactory (#10626)
* Initial plan * Apply PR #10624 changes to fix GitHubModels health check dependency on IHttpClientFactory Co-authored-by: sebastienros <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: sebastienros <[email protected]>
1 parent b6a45aa commit 9ba93cb

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Aspire.Hosting.GitHub.Models/GitHubModelsExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public static IResourceBuilder<GitHubModelResource> WithHealthCheck(this IResour
129129
var healthCheckKey = $"{builder.Resource.Name}_check";
130130
GitHubModelsHealthCheck? healthCheck = null;
131131

132+
// Ensure IHttpClientFactory is available by registering HTTP client services
133+
builder.ApplicationBuilder.Services.AddHttpClient();
134+
132135
// Register the health check
133136
builder.ApplicationBuilder.Services.AddHealthChecks()
134137
.Add(new HealthCheckRegistration(

tests/Aspire.Hosting.GitHub.Models.Tests/GitHubModelsExtensionTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Aspire.Hosting.ApplicationModel;
55
using Aspire.Hosting.Utils;
6+
using Microsoft.Extensions.DependencyInjection;
67
using Xunit;
78

89
namespace Aspire.Hosting.GitHub.Models.Tests;
@@ -288,4 +289,41 @@ public void WithHealthCheckAddsHealthCheckAnnotation()
288289
Assert.Single(healthCheckAnnotations);
289290
Assert.Equal("github_check", healthCheckAnnotations[0].Key);
290291
}
292+
293+
[Fact]
294+
public void WithHealthCheckEnsuresIHttpClientFactoryIsRegistered()
295+
{
296+
using var builder = TestDistributedApplicationBuilder.Create();
297+
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";
298+
299+
// Add the health check without explicitly calling AddHttpClient
300+
var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini").WithHealthCheck();
301+
302+
// Build the service provider to test dependency resolution
303+
var services = builder.Services.BuildServiceProvider();
304+
305+
// This should not throw because WithHealthCheck should ensure IHttpClientFactory is registered
306+
var httpClientFactory = services.GetService<IHttpClientFactory>();
307+
Assert.NotNull(httpClientFactory);
308+
}
309+
310+
[Fact]
311+
public void WithHealthCheckWorksWhenAddHttpClientIsCalledManually()
312+
{
313+
using var builder = TestDistributedApplicationBuilder.Create();
314+
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";
315+
316+
// Manually call AddHttpClient (should not conflict with automatic registration in WithHealthCheck)
317+
builder.Services.AddHttpClient();
318+
319+
// Add the health check
320+
var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini").WithHealthCheck();
321+
322+
// Build the service provider to test dependency resolution
323+
var services = builder.Services.BuildServiceProvider();
324+
325+
// This should work fine since AddHttpClient can be called multiple times safely
326+
var httpClientFactory = services.GetService<IHttpClientFactory>();
327+
Assert.NotNull(httpClientFactory);
328+
}
291329
}

0 commit comments

Comments
 (0)