dotnet / dotnet/aspnetcore

DefaultHealthCheckService throws exception

Open
#45,873 6 comments 0 reactions 0 assignees View on GitHub
area-healthchecks Needs: Attention :wave:
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Based on the code at [```HealthCheckPubliserHosterService``` ](https://github.com/dotnet/aspnetcore/blob/main/src/HealthChecks/HealthChecks/src/HealthCheckPublisherHostedService.cs#L154 ) DefaultHealthCheckService should not throw exceptions, but if there is exception on initialization of a [health check ](https://github.com/dotnet/aspnetcore/blob/843f1a76e770cdcd4e1b249f9f7d51ebf9e0e762/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs#L81) service doesn't properly handle it, which leads to failure of publishing health report.

For example if there is a dependency of a health check which is not registered in container, health check initialization would throw exception.

### Expected Behavior

`HealthCheckService` would return failed with failure of health check.

It's arguable whether failure of health check initialization should be reported the same as failure of logic within health check, but it must be reported in some way.

### Steps To Reproduce

Create a simple health check with missing dependency:
```
public class NonWorkingHealthCheck : IHealthCheck
{
public NonWorkingHealthCheck(string param, IDependency dep) {}

public Task CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
=> Task.FromResult(HealthCheckResult.Healthy());

interface IDependency {}
```

And register this health check:

```
builder.AddTypeActivatedCheck("NonWorking", "param1");
```

### Exceptions (if any)

_No response_

### .NET Version

7.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.