EntityFrameworkCoreHealthChecksBuilderExtensions.AddDbContextCheck<TContext> Method

Definition

Adds a health check for the specified DbContext type.

public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddDbContextCheck<TContext> (this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string? name = default, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = default, Func<TContext,System.Threading.CancellationToken,System.Threading.Tasks.Task<bool>>? customTestQuery = default) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextCheck : Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder * string * Nullable<Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus> * seq<string> * Func<'Context, System.Threading.CancellationToken, System.Threading.Tasks.Task<bool> (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)> -> Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextCheck(Of TContext As DbContext) (builder As IHealthChecksBuilder, Optional name As String = Nothing, Optional failureStatus As Nullable(Of HealthStatus) = Nothing, Optional tags As IEnumerable(Of String) = Nothing, Optional customTestQuery As Func(Of TContext, CancellationToken, Task(Of Boolean)) = Nothing) As IHealthChecksBuilder

Type Parameters

TContext

The DbContext type.

Parameters

name
String

The health check name. Optional. If null the type name of TContext will be used for the name.

failureStatus
Nullable<HealthStatus>

The HealthStatus that should be reported when the health check fails. Optional. If null then the default status of Unhealthy will be reported.

tags
IEnumerable<String>

A list of tags that can be used to filter sets of health checks. Optional.

customTestQuery
Func<TContext,CancellationToken,Task<Boolean>>

A custom test query that will be executed when the health check executes to test the health of the database connection and configurations.

Returns

The IHealthChecksBuilder.

Remarks

The health check implementation added by this method will use the dependency injection container to create an instance of TContext.

By default the health check implementation will use the CanConnectAsync(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.

Providing a customTestQuery will replace the use of CanConnectAsync(CancellationToken) to test database connectivity. An implementation of a test query should handle exceptions that can arise due to connectivity failure, and should return a pass/fail result. The test query should be be designed to complete in a short and predicatable amount of time.

Applies to