© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
65 replies
TeBeCo

✅ [ASPNETCORE - HealthCheck - Timeout] Trying to find a more elegant way to write this

ok soooo question about HealthCheck:
I'm trying to configure the
Timeout
Timeout
through
IOptions<>
IOptions<>
which is bound to my configuration
* of course reading
IConfiguration.GetValue<>
IConfiguration.GetValue<>
is a NO GO here
* added code for "lol harcode / not configurable" way
* added code for equivalent which uses configuration binding through options

Am I missing a better way ?

public class Foo : IHealthCheck
{
    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = default)
        => await Task.Delay(2000, ct);  return HealthCheckResult.Healthy("");
}
public class FooOptions
{
    public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(1);
}

/////////
// I hate it because it's not configurable at startup time:
// builder.Services.AddHealthChecks()
//                .AddCheck<Foo>("foo", null, null, TimeSpan.FromSeconds(1));
/////////


// Workaround:
builder.Services.AddHealthChecks()
                .AddCheck<Foo>("foo");
builder.Services.AddOptions<HealthCheckServiceOptions>()
                .PostConfigure<IOptions<FooOptions>>((options, myOptions) =>
                {
                    var registrations = options.Registrations.Where(registration => registration.Name == "foo");
                    foreach (var registration in registrations)
                    {
                        registration.Timeout = myOptions.Value.Timeout;
                    }
                });
public class Foo : IHealthCheck
{
    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = default)
        => await Task.Delay(2000, ct);  return HealthCheckResult.Healthy("");
}
public class FooOptions
{
    public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(1);
}

/////////
// I hate it because it's not configurable at startup time:
// builder.Services.AddHealthChecks()
//                .AddCheck<Foo>("foo", null, null, TimeSpan.FromSeconds(1));
/////////


// Workaround:
builder.Services.AddHealthChecks()
                .AddCheck<Foo>("foo");
builder.Services.AddOptions<HealthCheckServiceOptions>()
                .PostConfigure<IOptions<FooOptions>>((options, myOptions) =>
                {
                    var registrations = options.Registrations.Where(registration => registration.Name == "foo");
                    foreach (var registration in registrations)
                    {
                        registration.Timeout = myOptions.Value.Timeout;
                    }
                });
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

More Elegant Way to Make a Generic Lookup Type
C#CC# / help
2y ago
Is there a better way to write this QueryBuilder?
C#CC# / help
16mo ago
✅ Trying to round a value but there's seemingly no elegant way to do it?
C#CC# / help
3y ago
Am i on a proper way to learn AspNetCore?
C#CC# / help
4mo ago