DelegateHealthCheck /8#31357
Conversation
| builder.Services.AddHealthChecks() | ||
| .Add(new HealthCheckRegistration( | ||
| name: "SampleHealthCheck1", | ||
| instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))), |
There was a problem hiding this comment.
Error CS0122 'DelegateHealthCheck' is inaccessible due to its protection level
cc @francedot @JuergenGutsch
There was a problem hiding this comment.
https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/DelegateHealthCheck.cs
The DelegateHealthCheck is an internal class
There was a problem hiding this comment.
Good catch. Instead of DelegateHealthCheck, you should be able to pass on a custom implementation of IHealthCheck as instance. The aspnetcore docs should therefore include the following to instruct how to use the new Periodic HealthChecks:
// PeriodicHealthCheck.cs or any other implementation
public class PeriodicHealthCheck : IHealthCheck
{
private readonly Func<CancellationToken, Task<HealthCheckResult>> _check;
public PeriodicHealthCheck (Func<CancellationToken, Task<HealthCheckResult>> check)
{
_check = check ?? throw new ArgumentNullException(nameof(check));
}
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) => _check(cancellationToken);
}
// Startup.cs or Program.cs
builder.Services.AddHealthChecks()
.Add(new HealthCheckRegistration(
name: "SampleHealthCheck1",
instance: new PeriodicHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))),
failureStatus: null,
tags: null,
timeout: default)
{
Delay = TimeSpan.FromSeconds(40),
Period = TimeSpan.FromSeconds(30)
});There was a problem hiding this comment.
The best way to add a delegate as a health check is to just use AddCheck or AddAsyncCheck defined in the HealthChecksBuilderDelegateExtensions:
public static void MapHealthChecksComplete2(string[] args)
{
// <snippet_MapHealthChecksComplete2>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.AddAsyncCheck(name: "SampleHealthCheck1",
check: _ => Task.FromResult(HealthCheckResult.Healthy("HealthyMessage")),
tags: null,
timeout: default);
var app = builder.Build();
app.MapHealthChecks("/healthz");
app.Run();
// </snippet_MapHealthChecksComplete2>
}
There was a problem hiding this comment.
Can I push directly into the hc2 branch? If not the method shown above should work
There was a problem hiding this comment.
What @francedot proposed:
public static void MapHealthChecksComplete2(string[] args)
{
// <snippet_MapHealthChecksComplete2>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.Add(new HealthCheckRegistration(
name: "SampleHealthCheck1",
instance: new SampleHealthCheck(),
failureStatus: null,
tags: null,
timeout: default)
{
Delay = TimeSpan.FromSeconds(40),
Period = TimeSpan.FromSeconds(30)
});
var app = builder.Build();
app.MapHealthChecks("/healthz");
app.Run();
// </snippet_MapHealthChecksComplete2>
}
| builder.Services.AddHealthChecks() | ||
| .Add(new HealthCheckRegistration( | ||
| name: "SampleHealthCheck1", | ||
| instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))), |
There was a problem hiding this comment.
The best way to add a delegate as a health check is to just use AddCheck or AddAsyncCheck defined in the HealthChecksBuilderDelegateExtensions:
public static void MapHealthChecksComplete2(string[] args)
{
// <snippet_MapHealthChecksComplete2>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
.AddAsyncCheck(name: "SampleHealthCheck1",
check: _ => Task.FromResult(HealthCheckResult.Healthy("HealthyMessage")),
tags: null,
timeout: default);
var app = builder.Build();
app.MapHealthChecks("/healthz");
app.Run();
// </snippet_MapHealthChecksComplete2>
}
| builder.Services.AddHealthChecks() | ||
| .Add(new HealthCheckRegistration( | ||
| name: "SampleHealthCheck1", | ||
| instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))), |
There was a problem hiding this comment.
Can I push directly into the hc2 branch? If not the method shown above should work
Try it, I don't think you have permission. |
|
@JuergenGutsch @francedot that fixed it, THANKS. |
|
@francedot @JuergenGutsch please review. Public review of the doc build:
|
|
@Rick-Anderson
I would propose something like this:
|
Much better. |
Fixes #31143
Error CS0122 'DelegateHealthCheck' is inaccessible due to its protection level
cc @francedot @JuergenGutsch @JoshKeegan @tdykstra @viktorpeacock
Internal previews