Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Performance: http client improvements #1021

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 8 additions & 17 deletions src/Web/HealthChecks/ApiHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -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> baseUrlConfiguration, IHttpClientFactory httpClientFactory) : IHealthCheck
{
private readonly BaseUrlConfiguration _baseUrlConfiguration;
private readonly BaseUrlConfiguration _baseUrlConfiguration = baseUrlConfiguration.Value;
private HttpClient? _httpClient;

public ApiHealthCheck(IOptions<BaseUrlConfiguration> baseUrlConfiguration)
{
_baseUrlConfiguration = baseUrlConfiguration.Value;
}

public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default(CancellationToken))
public async Task<HealthCheckResult> 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.");
Expand Down
22 changes: 7 additions & 15 deletions src/Web/HealthChecks/HomePageHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
_httpContextAccessor = httpContextAccessor;
}

public async Task<HealthCheckResult> 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.");
Expand Down
7 changes: 3 additions & 4 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@
var baseUrlConfig = configSection.Get<BaseUrlConfiguration>();

// Blazor Admin Required Services for Prerendering
builder.Services.AddScoped<HttpClient>(s => new HttpClient
{
BaseAddress = new Uri(baseUrlConfig!.WebBase)
});

builder.Services.AddHttpClient();


// add blazor services
builder.Services.AddBlazoredLocalStorage();
Expand Down