Created
March 18, 2022 14:53
-
-
Save benhysell/5fd792514d6204f32ae5bedb90e41276 to your computer and use it in GitHub Desktop.
asp.net core health check controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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