Specific health checks implementations registered into the application (#619)
This commit is contained in:
@@ -1,31 +1,25 @@
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using BlazorShared;
|
||||||
using Microsoft.AspNetCore.Routing;
|
|
||||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.Web.HealthChecks;
|
namespace Microsoft.eShopWeb.Web.HealthChecks;
|
||||||
|
|
||||||
public class ApiHealthCheck : IHealthCheck
|
public class ApiHealthCheck : IHealthCheck
|
||||||
{
|
{
|
||||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
private readonly BaseUrlConfiguration _baseUrlConfiguration;
|
||||||
private readonly LinkGenerator _linkGenerator;
|
|
||||||
|
|
||||||
public ApiHealthCheck(IHttpContextAccessor httpContextAccessor, LinkGenerator linkGenerator)
|
public ApiHealthCheck(BaseUrlConfiguration baseUrlConfiguration)
|
||||||
{
|
{
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_baseUrlConfiguration = baseUrlConfiguration;
|
||||||
_linkGenerator = linkGenerator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||||
HealthCheckContext context,
|
HealthCheckContext context,
|
||||||
CancellationToken cancellationToken = default(CancellationToken))
|
CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
var request = _httpContextAccessor.HttpContext.Request;
|
string myUrl = _baseUrlConfiguration.ApiBase + "catalog-items";
|
||||||
|
|
||||||
string apiLink = _linkGenerator.GetPathByAction("List", "Catalog");
|
|
||||||
string myUrl = request.Scheme + "://" + request.Host.ToString() + apiLink;
|
|
||||||
var client = new HttpClient();
|
var client = new HttpClient();
|
||||||
var response = await client.GetAsync(myUrl);
|
var response = await client.GetAsync(myUrl);
|
||||||
var pageContents = await response.Content.ReadAsStringAsync();
|
var pageContents = await response.Content.ReadAsStringAsync();
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
|||||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||||
using Microsoft.eShopWeb.Infrastructure.Identity;
|
using Microsoft.eShopWeb.Infrastructure.Identity;
|
||||||
using Microsoft.eShopWeb.Web.Configuration;
|
using Microsoft.eShopWeb.Web.Configuration;
|
||||||
|
using Microsoft.eShopWeb.Web.HealthChecks;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
@@ -136,7 +137,10 @@ public class Startup
|
|||||||
options.Conventions.AuthorizePage("/Basket/Checkout");
|
options.Conventions.AuthorizePage("/Basket/Checkout");
|
||||||
});
|
});
|
||||||
services.AddHttpContextAccessor();
|
services.AddHttpContextAccessor();
|
||||||
services.AddHealthChecks();
|
services
|
||||||
|
.AddHealthChecks()
|
||||||
|
.AddCheck<ApiHealthCheck>("api_health_check", tags: new[] { "apiHealthCheck" })
|
||||||
|
.AddCheck<HomePageHealthCheck>("home_page_health_check", tags: new[] { "homePageHealthCheck" });
|
||||||
services.Configure<ServiceConfig>(config =>
|
services.Configure<ServiceConfig>(config =>
|
||||||
{
|
{
|
||||||
config.Services = new List<ServiceDescriptor>(services);
|
config.Services = new List<ServiceDescriptor>(services);
|
||||||
@@ -227,8 +231,8 @@ public class Startup
|
|||||||
{
|
{
|
||||||
endpoints.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
|
endpoints.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
|
||||||
endpoints.MapRazorPages();
|
endpoints.MapRazorPages();
|
||||||
endpoints.MapHealthChecks("home_page_health_check");
|
endpoints.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") });
|
||||||
endpoints.MapHealthChecks("api_health_check");
|
endpoints.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") });
|
||||||
//endpoints.MapBlazorHub("/admin");
|
//endpoints.MapBlazorHub("/admin");
|
||||||
endpoints.MapFallbackToFile("index.html");
|
endpoints.MapFallbackToFile("index.html");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user