Skip to content

Commit eb757d3

Browse files
authored
Specific health checks implementations registered into the application (dotnet-architecture#619)
1 parent 77d9e4c commit eb757d3

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/Web/HealthChecks/ApiHealthCheck.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
using System.Net.Http;
22
using System.Threading;
33
using System.Threading.Tasks;
4-
using Microsoft.AspNetCore.Http;
5-
using Microsoft.AspNetCore.Routing;
4+
using BlazorShared;
65
using Microsoft.Extensions.Diagnostics.HealthChecks;
76

87
namespace Microsoft.eShopWeb.Web.HealthChecks;
98

109
public class ApiHealthCheck : IHealthCheck
1110
{
12-
private readonly IHttpContextAccessor _httpContextAccessor;
13-
private readonly LinkGenerator _linkGenerator;
11+
private readonly BaseUrlConfiguration _baseUrlConfiguration;
1412

15-
public ApiHealthCheck(IHttpContextAccessor httpContextAccessor, LinkGenerator linkGenerator)
13+
public ApiHealthCheck(BaseUrlConfiguration baseUrlConfiguration)
1614
{
17-
_httpContextAccessor = httpContextAccessor;
18-
_linkGenerator = linkGenerator;
15+
_baseUrlConfiguration = baseUrlConfiguration;
1916
}
2017

2118
public async Task<HealthCheckResult> CheckHealthAsync(
2219
HealthCheckContext context,
2320
CancellationToken cancellationToken = default(CancellationToken))
2421
{
25-
var request = _httpContextAccessor.HttpContext.Request;
26-
27-
string apiLink = _linkGenerator.GetPathByAction("List", "Catalog");
28-
string myUrl = request.Scheme + "://" + request.Host.ToString() + apiLink;
22+
string myUrl = _baseUrlConfiguration.ApiBase + "catalog-items";
2923
var client = new HttpClient();
3024
var response = await client.GetAsync(myUrl);
3125
var pageContents = await response.Content.ReadAsStringAsync();

src/Web/Startup.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.eShopWeb.Infrastructure.Data;
2323
using Microsoft.eShopWeb.Infrastructure.Identity;
2424
using Microsoft.eShopWeb.Web.Configuration;
25+
using Microsoft.eShopWeb.Web.HealthChecks;
2526
using Microsoft.Extensions.Configuration;
2627
using Microsoft.Extensions.DependencyInjection;
2728
using Microsoft.Extensions.Diagnostics.HealthChecks;
@@ -136,7 +137,10 @@ public void ConfigureServices(IServiceCollection services)
136137
options.Conventions.AuthorizePage("/Basket/Checkout");
137138
});
138139
services.AddHttpContextAccessor();
139-
services.AddHealthChecks();
140+
services
141+
.AddHealthChecks()
142+
.AddCheck<ApiHealthCheck>("api_health_check", tags: new[] { "apiHealthCheck" })
143+
.AddCheck<HomePageHealthCheck>("home_page_health_check", tags: new[] { "homePageHealthCheck" });
140144
services.Configure<ServiceConfig>(config =>
141145
{
142146
config.Services = new List<ServiceDescriptor>(services);
@@ -227,8 +231,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
227231
{
228232
endpoints.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
229233
endpoints.MapRazorPages();
230-
endpoints.MapHealthChecks("home_page_health_check");
231-
endpoints.MapHealthChecks("api_health_check");
234+
endpoints.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") });
235+
endpoints.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") });
232236
//endpoints.MapBlazorHub("/admin");
233237
endpoints.MapFallbackToFile("index.html");
234238
});

0 commit comments

Comments
 (0)