Created
December 25, 2018 13:31
-
-
Save explorer14/00d61b51905a490ce2c2545bcfce9157 to your computer and use it in GitHub Desktop.
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.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