Skip to content

Instantly share code, notes, and snippets.

@benhysell
Created March 18, 2022 14:53
Show Gist options
  • Save benhysell/5fd792514d6204f32ae5bedb90e41276 to your computer and use it in GitHub Desktop.
Save benhysell/5fd792514d6204f32ae5bedb90e41276 to your computer and use it in GitHub Desktop.
asp.net core health check controller
using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.SignalR.Client;
namespace UI
{
public partial class DisplayResults : IDisposable
{
private HubConnection _hubConnection;
private HealthStatusDto _healthStatus = new HealthStatusDto() { HealthStatus = Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy };
protected override async Task OnInitializedAsync()
{
_hubConnection = new HubConnectionBuilder().WithUrl(NavigationManager.ToAbsoluteUri("/hubs/healthcheck"), options =>
{
//options.AccessTokenProvider = async () =>
//{
// var x = await StateProvider.GetAuthenticationStateAsync();
// return x.GetClaim("access_token");
//};
})
.Build();
_hubConnection.On<HealthStatusDto>("ReceiveRefreshMessageAsync", (healthCheckStatus) =>
{
_healthStatus = healthCheckStatus;
if (healthCheckStatus.HealthStatus == Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Degraded || healthCheckStatus.HealthStatus == Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy)
{
StateHasChanged();
}
});
await _hubConnection.StartAsync();
}
public async ValueTask DisposeAsync()
{
await _hubConnection.DisposeAsync();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Hubs.HealthCheck
{
/// <summary>
/// Health Check
/// </summary>
//[Authorize]
public class HealthCheckHub : Hub
{
/// <summary>
/// Send Refresh Message
/// </summary>
/// <returns></returns>
public async Task SendRefreshMessageAsync(HealthStatusDto updatedEntity)
{
await Clients.All.SendAsync("ReceiveRefreshMessageAsync", updatedEntity);
}
}
}
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
namespace Controllers.HealthCheck
{
[ApiController]
[ApiVersion("1.0")]
[Produces("application/json")]
[Route("api/v{version:apiVersion}/[controller]")]
public class HealthController : ControllerBase
{
private readonly ILogger<HealthController> _logger;
private readonly IHubContext<HealthCheckHub> _healthCheckHub;
private readonly HealthCheckService _healthCheckService;
public HealthController(ILogger<HealthController> logger, HealthCheckService healthCheckService, IHubContext<HealthCheckHub> healthCheckHub)
{
_healthCheckService = healthCheckService;
_logger = logger;
_healthCheckHub = healthCheckHub;
}
[HttpGet]
[AllowAnonymous]
public async Task GetAsync()
{
var report = await _healthCheckService.CheckHealthAsync();
var entityDto = new HealthStatusDto() { HealthStatus = report.Status, LastUpdateDateTime = System.DateTime.Now };
await _healthCheckHub.Clients.All.SendAsync("ReceiveRefreshMessageAsync", entityDto);
await HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse(HttpContext, report);
}
}
}
using Newtonsoft.Json;
using System;
namespace Dto
{
/// <summary>
/// HealthStatusDto
/// </summary>
public class HealthStatusDto
{
public Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus HealthStatus { get; set; }
public string StatusMessage { get; set; }
public DateTime LastUpdateDateTime { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment