Add new IHealthChecksBuilder extension method.
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
## Background and Motivation
I frequently find myself needing to use the `Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder.Add(HealthCheckRegistration registration)` extension method to register health checks that require use of the `IServiceProvider` to setup the Health check dependencies. It would be nice to have an API for this scenario for registering health checks in consistency with the other extension methods, such as:
```csharp
public static IHealthChecksBuilder AddCheck(
this IHealthChecksBuilder builder,
string name,
HealthStatus? failureStatus,
IEnumerable tags) where T : class, IHealthCheck
```
etc...
## Proposed API
The API I would like to propose is as follows:
```csharp
namespace Microsoft.Extensions.DependencyInjection;
public static class HealthChecksBuilderAddCheckExtensions
{
// existing code omitted for brevity
public static IHealthChecksBuilder AddCheck(
this IHealthChecksBuilder builder,
string name,
Func factory,
HealthStatus? failureStatus = null,
IEnumerable? tags = null) where T : class, IHealthCheck
{
return builder.Add(new HealthCheckRegistration(name, factory, failureStatus, tags));
}
}
```
## Usage Examples
The usage of this API would simplify registering of a health check that requires the use of the `IServiceProvider`:
```csharp
services.AddHealthChecks()
.AddCheck("DbConnectionCheck", sp =>
{
var configuration = sp.GetRequiredService();
var connectionString = configuration.GetConnectionString("DefaultConnectionString");
var provider = configuration.GetValue("Configuration:DbProvider");
return new DbConnectionCheck(connectionString, dbProvider);
}, HealthStatus.Unhealthy, new[] { "dependency", "db-dependency" });
```
## Alternative Designs
None
## Risks
No known risks.
Contributor guide
Assessment
This issue has not been assessed yet.