diff --git a/src/Web/HealthChecks/ApiHealthCheck.cs b/src/Web/HealthChecks/ApiHealthCheck.cs index 49f6634e5..58c55d993 100644 --- a/src/Web/HealthChecks/ApiHealthCheck.cs +++ b/src/Web/HealthChecks/ApiHealthCheck.cs @@ -1,29 +1,20 @@ -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using BlazorShared; +using BlazorShared; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; namespace Microsoft.eShopWeb.Web.HealthChecks; -public class ApiHealthCheck : IHealthCheck +public class ApiHealthCheck(IOptions baseUrlConfiguration, IHttpClientFactory httpClientFactory) : IHealthCheck { - private readonly BaseUrlConfiguration _baseUrlConfiguration; + private readonly BaseUrlConfiguration _baseUrlConfiguration = baseUrlConfiguration.Value; + private HttpClient? _httpClient; - public ApiHealthCheck(IOptions baseUrlConfiguration) - { - _baseUrlConfiguration = baseUrlConfiguration.Value; - } - - public async Task CheckHealthAsync( - HealthCheckContext context, - CancellationToken cancellationToken = default(CancellationToken)) + public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { + _httpClient = httpClientFactory.CreateClient(); string myUrl = _baseUrlConfiguration.ApiBase + "catalog-items"; - var client = new HttpClient(); - var response = await client.GetAsync(myUrl); - var pageContents = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.GetAsync(myUrl, cancellationToken); + var pageContents = await response.Content.ReadAsStringAsync(cancellationToken); if (pageContents.Contains(".NET Bot Black Sweatshirt")) { return HealthCheckResult.Healthy("The check indicates a healthy result."); diff --git a/src/Web/HealthChecks/HomePageHealthCheck.cs b/src/Web/HealthChecks/HomePageHealthCheck.cs index 0896954db..1cf31932e 100644 --- a/src/Web/HealthChecks/HomePageHealthCheck.cs +++ b/src/Web/HealthChecks/HomePageHealthCheck.cs @@ -6,25 +6,17 @@ namespace Microsoft.eShopWeb.Web.HealthChecks; -public class HomePageHealthCheck : IHealthCheck +public class HomePageHealthCheck(IHttpContextAccessor httpContextAccessor, IHttpClientFactory httpClientFactory) : IHealthCheck { - private readonly IHttpContextAccessor _httpContextAccessor; + private HttpClient? _httpClient; - public HomePageHealthCheck(IHttpContextAccessor httpContextAccessor) + public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { - _httpContextAccessor = httpContextAccessor; - } - - public async Task CheckHealthAsync( - HealthCheckContext context, - CancellationToken cancellationToken = default(CancellationToken)) - { - var request = _httpContextAccessor.HttpContext?.Request; + _httpClient = httpClientFactory.CreateClient(); + var request = httpContextAccessor.HttpContext?.Request; string myUrl = request?.Scheme + "://" + request?.Host.ToString(); - - var client = new HttpClient(); - var response = await client.GetAsync(myUrl); - var pageContents = await response.Content.ReadAsStringAsync(); + var response = await _httpClient.GetAsync(myUrl, cancellationToken); + var pageContents = await response.Content.ReadAsStringAsync(cancellationToken); if (pageContents.Contains(".NET Bot Black Sweatshirt")) { return HealthCheckResult.Healthy("The check indicates a healthy result."); diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 9761361dd..34606a66f 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -99,10 +99,9 @@ var baseUrlConfig = configSection.Get(); // Blazor Admin Required Services for Prerendering -builder.Services.AddScoped(s => new HttpClient -{ - BaseAddress = new Uri(baseUrlConfig!.WebBase) -}); + +builder.Services.AddHttpClient(); + // add blazor services builder.Services.AddBlazoredLocalStorage();