ResourceBuilderExtensions.WithHealthCheck<T> Method

Definition

Adds a HealthCheckAnnotation to the resource annotations to associate a resource with a named health check managed by the health check service.

public static Aspire.Hosting.ApplicationModel.IResourceBuilder<T> WithHealthCheck<T> (this Aspire.Hosting.ApplicationModel.IResourceBuilder<T> builder, string key) where T : Aspire.Hosting.ApplicationModel.IResource;
static member WithHealthCheck : Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.IResource)> * string -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.IResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.IResource)
<Extension()>
Public Function WithHealthCheck(Of T As IResource) (builder As IResourceBuilder(Of T), key As String) As IResourceBuilder(Of T)

Type Parameters

T

The type of the resource.

Parameters

builder
IResourceBuilder<T>

The resource builder.

key
String

The key for the health check.

Returns

The resource builder.

Examples

Define a custom health check and associate it with a resource.

var builder = DistributedApplication.CreateBuilder(args);

var startAfter = DateTime.Now.AddSeconds(30);

builder.Services.AddHealthChecks().AddCheck(mycheck", () =>
{
    return DateTime.Now > startAfter ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();
});

var pg = builder.AddPostgres("pg")
                .WithHealthCheck("mycheck");

builder.AddProject<Projects.MyApp>("myapp")
       .WithReference(pg)
       .WaitFor(pg); // This will result in waiting for the building check, and the
                     // custom check defined in the code.

Remarks

The WithHealthCheck<T>(IResourceBuilder<T>, String) method is used in conjunction with the WaitFor<T>(IResourceBuilder<T>, IResourceBuilder<IResource>) to associate a resource registered in the application hosts dependency injection container. The WithHealthCheck<T>(IResourceBuilder<T>, String) method does not inject the health check itself it is purely an association mechanism.

Applies to