Skip to content

Add AddHealthChecks(Action<HealthCheckServiceOptions>) overload#66774

Open
KitKeen wants to merge 3 commits into
dotnet:mainfrom
KitKeen:feat/health-checks-configure-options
Open

Add AddHealthChecks(Action<HealthCheckServiceOptions>) overload#66774
KitKeen wants to merge 3 commits into
dotnet:mainfrom
KitKeen:feat/health-checks-configure-options

Conversation

@KitKeen
Copy link
Copy Markdown

@KitKeen KitKeen commented May 21, 2026

Fixes #8530.

Summary

services.AddHealthChecks() returns an IHealthChecksBuilder, but does not currently expose a convenient way to configure HealthCheckServiceOptions (which DefaultHealthCheckService consumes via IOptions<HealthCheckServiceOptions>). Users today have to call services.Configure<HealthCheckServiceOptions>(...) separately and then services.AddHealthChecks() — two statements instead of one, and the relationship between them is not discoverable from the API surface.

This PR adds a new overload that takes a configuration delegate:

public static IHealthChecksBuilder AddHealthChecks(
    this IServiceCollection services,
    Action<HealthCheckServiceOptions> configureOptions);

Usage:

services.AddHealthChecks(options =>
{
    options.Registrations.Add(new HealthCheckRegistration(
        "marker",
        _ => Task.FromResult(HealthCheckResult.Healthy()),
        failureStatus: null,
        tags: null));
})
.AddCheck<MyHealthCheck>("my-check");

The overload internally calls services.Configure(configureOptions) and then delegates to the existing parameterless AddHealthChecks(), so the idempotency guarantee and the existing service registrations are preserved. The returned IHealthChecksBuilder remains usable for chained registrations.

Changes

  • HealthCheckServiceCollectionExtensions.cs — new overload with full XML docs.
  • PublicAPI.Unshipped.txt — baselined the new API.
  • ServiceCollectionExtensionsTest.cs — added four tests:
    • delegate is invoked on resolved options,
    • returned builder is usable for further registrations alongside the delegate,
    • null services throws ArgumentNullException,
    • null configureOptions throws ArgumentNullException.

@github-actions github-actions Bot added the area-healthchecks Includes: Healthchecks (some bugs also in Extensions repo) label May 21, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 21, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Thanks for your PR, @KitKeen. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-healthchecks Includes: Healthchecks (some bugs also in Extensions repo) community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

services.AddHealthChecks missing overload that takes delegate for configuring HealthChecksOptions

1 participant