Added HealthCheck (#188)

Checks home page.
This commit is contained in:
Steve Smith
2019-01-11 13:27:15 -05:00
committed by GitHub
parent c64c70b183
commit 84f1eeee8c
2 changed files with 42 additions and 3 deletions

View File

@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Web.HealthChecks
{
public class HomePageHealthCheck : IHealthCheck
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HomePageHealthCheck(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default(CancellationToken))
{
string myUrl = _httpContextAccessor.HttpContext.Request.Host.ToString();
var client = new HttpClient();
var response = await client.GetAsync(myUrl);
var pageContents = await response.Content.ReadAsStringAsync();
if (pageContents.Contains(".NET Bot Black Sweatshirt"))
{
return HealthCheckResult.Healthy("The check indicates a healthy result.");
}
return HealthCheckResult.Unhealthy("The check indicates an unhealthy result.");
}
}
}