This file contains 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.EntityFrameworkCore; | |
using Microsoft.Extensions.Diagnostics.HealthChecks; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public class DatabaseHealthCheck : IHealthCheck | |
{ | |
private readonly DbContext dbContext; | |
public DatabaseHealthCheck(DbContext context) | |
{ | |
this.dbContext = context; | |
} | |
public async Task<HealthCheckResult> CheckHealthAsync( | |
HealthCheckContext context, | |
CancellationToken cancellationToken = default(CancellationToken)) | |
{ | |
var canConnectToDatabase = | |
await dbContext.Database.CanConnectAsync(); | |
if (canConnectToDatabase) | |
return HealthCheckResult.Healthy(); | |
return HealthCheckResult.Unhealthy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment