Skip to content

Instantly share code, notes, and snippets.

@explorer14
Created December 25, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save explorer14/00d61b51905a490ce2c2545bcfce9157 to your computer and use it in GitHub Desktop.
Save explorer14/00d61b51905a490ce2c2545bcfce9157 to your computer and use it in GitHub Desktop.
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