ResourceBuilderExtensions.WithHealthCheck<T> Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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)
- T
The type of the resource.
- builder
- IResourceBuilder<T>
The resource builder.
- key
- String
The key for the health check.
The IResourceBuilder<T>.
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.
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.
Product | Versions |
---|---|
.NET Aspire | 9.0.0, 9.1.0 |