Health Check EFCore Database in ASP.NET Core
In this article, we shall check how to check the Health Check Entity Framework Database in ASP.NET Core.
A health check is critical in many aspects of the application life cycle.
It allows you to monitor real-time information on the health of various resources, databases, and services your business application is dependent on.
This ultimately helps you in troubleshooting and resolving technical or environmental issues easily.
Today in this article, we will cover below aspects,
In ASP.Net core Health check services and health check, middleware provides us capabilities to validate external resources connectivity in your application ( Example: SQL Server database).
These simple and easy-to-use feature lets you check the status of your resources and dependencies.
Getting started
If you already have an API, then please add the below Nuget Package to your application.
Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore
Install it using NPM (Nuget Package Manager)
OR
Install it using Package Manager Console
PM> Install-Package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore -Version 3.1.3
Kindly perform the below 2 steps to enable health check into your API,
Step 1 – Register EFCore HealthCheck Service
Let’s register the Health check service in the API as below. Please add the below line of code in the ConfigureServices() method,
services.AddHealthChecks().AddDbContextCheck<TheCodeBuzzContext>();
AddDbContextCheck extension builder method,
- AddDbContextCheck is EntityFrameworkCore HealthChecks BuilderExtensions method which adds a health check for the specified DbContext type.
- This method uses the dependency injection((DI) container to create an instance of your context type i.e TContext.
- This method uses a health check implementation CanConnectAsync(System.Threading.CancellationToken) method to test connectivity to the database.
- This method requires that the database provider has correctly implemented the IDatabaseCreator interface. If the database provider has not implemented this interface then the health check will report a failure.
TheCodeBuzzContext – This is the context class that will be created by the scaffolding command. Please see here for more details.
Here is the complete code,
Step 2 – Add Health Check endpoint middleware
Enable Health Check endpoint middleware in the API pipeline as below,
app.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/EFCoreHealth"); });
Verify health check endpoint
Let’s execute the HealthCheck endpoint,
Status: Healthy
Status: UnHealthy
Let me drop the database in the SQL server,
That’s All, Enjoy Coding !!
Please sound off your comments below if any.
References:
Summary
A health check is critical in multiple aspects and allows you to check the health of various resources, databases, and services your application is dependent on. Today in this article we learned how to configure Health Check for Entity Framework Database in the ASP.NET Core c# application.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
This worked great with EF 3.x. But when I moved to 5.x it started throwing this error.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating HealthChecks.UI.Core.Data.HealthChecksDb.
—> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor ‘Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1[HealthChecks.UI.Core.Data.HealthChecksDb])’ on type ‘HealthChecksDb’.
—> System.TypeLoadException: Method ‘CommitTransactionAsync’ in type ‘Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTransactionManager’ from assembly ‘Microsoft.EntityFrameworkCore.InMemory, Version=3.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’ does not have an implementation.
Hi Randy Kreisel- Thanks for your query. I shall have a look. I see you are using Autofac as a dependency injection framework. Did you check its compatibility issues with .NET core if any to start with?