Skip to content

DelegateHealthCheck /8#31357

Merged
Rick-Anderson merged 9 commits into
mainfrom
hc2
Jan 10, 2024
Merged

DelegateHealthCheck /8#31357
Rick-Anderson merged 9 commits into
mainfrom
hc2

Conversation

@Rick-Anderson
Copy link
Copy Markdown
Contributor

@Rick-Anderson Rick-Anderson commented Jan 5, 2024

Fixes #31143

Error CS0122 'DelegateHealthCheck' is inaccessible due to its protection level
cc @francedot @JuergenGutsch @JoshKeegan @tdykstra @viktorpeacock


Internal previews

📄 File 🔗 Preview link
aspnetcore/host-and-deploy/health-checks.md Health checks in ASP.NET Core

builder.Services.AddHealthChecks()
.Add(new HealthCheckRegistration(
name: "SampleHealthCheck1",
instance: new DelegateHealthCheck(_ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage))),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error CS0122 'DelegateHealthCheck' is inaccessible due to its protection level
cc @francedot @JuergenGutsch

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

@francedot francedot Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
          });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I push directly into the hc2 branch? If not the method shown above should work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I push directly into the hc2 branch? If not the method shown above should work

@Rick-Anderson
Copy link
Copy Markdown
Contributor Author

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.

@Rick-Anderson
Copy link
Copy Markdown
Contributor Author

@JuergenGutsch @francedot that fixed it, THANKS.

@Rick-Anderson
Copy link
Copy Markdown
Contributor Author

@francedot @JuergenGutsch please review.

Public review of the doc build:

  • download the HTML files
  • unzip
  • view HTML file in a browser.
  • For links outside the doc, remove the review. from URL.

@JuergenGutsch
Copy link
Copy Markdown
Contributor

@Rick-Anderson
I'm not sure if this sentence makes sense anymore since the DelegateHealthCheck isn't used directly:

The DelegateHealthCheck class enables setting Delay and Period on each health check individually.

I would propose something like this:

You can set Delay and Period on each HealthCheckRegistration individually.

@Rick-Anderson
Copy link
Copy Markdown
Contributor Author

uld propose something like this:

You can set Delay and Period on each HealthCheckRegistration individually.

Much better.

@Rick-Anderson Rick-Anderson merged commit e6f933a into main Jan 10, 2024
@Rick-Anderson Rick-Anderson deleted the hc2 branch January 10, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing sample for new feature in ASP.NET Core 8

3 participants