3

I have this code which can be used to check the database health using C# in NET 6.

Version 1:

var hcBuilder = builder.Services.AddHealthChecks();    
hcBuilder.AddSqlServer(myServicesConnStr);

Version 2:

builder.Services.AddHealthChecks()
    .AddDbContextCheck<MyServicesContext>();

What is the difference between version 1 and version 2 in regards to checking the health of the database using HealthCheck package? Is there any difference in its efficiency?

Steve
  • 2,963
  • 15
  • 61
  • 133

2 Answers2

3

Version 1. from AspNetCore.HealthChecks.SqlServer : It is not maintained or supported by Microsoft.

Version 2. from Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore : It is maintained and supported by Microsoft.

mkmkmk
  • 167
  • 4
0

AddSqlServer uses a SqlConnection directly under the hood.

AddDbContextCheck will use a DbContext with its configuration to do a query. It will use the configured execution strategy for the DbContext as well, so if you are registering it with EnableRetryOnFailure() then the health check query will also be retried in case of a connection failure.

sveinungf
  • 841
  • 1
  • 12
  • 22