Skip to content

Health propagation between API an UI not possible due to sealed & un(de)serializable #58541

Open
@ibruynin

Description

@ibruynin

I'm trying to propagate the degraded health from the API to the UI (using that API) as follows:

API:8080/health returns JsonSerializer.Serialize<HealthReport>(result)

UI:8080/health returns health info based on the result of the API

var s = await response.Content.ReadAsStringAsync();                
var hr = JsonNode.Parse(s).AsObject();
var entries = hr["Entries"];
var entry = entries["API_XYZ"];
var hres = Int32.Parse(entry["Status"].ToString());
HealthStatus status = (HealthStatus)hres;
return status switch
{
    HealthStatus.Healthy => HealthCheckResult.Healthy("Degraded API up"),
    HealthStatus.Degraded => HealthCheckResult.Degraded("Degraded API up, but degraded"),
    HealthStatus.Unhealthy => HealthCheckResult.Unhealthy("Degraded API unavailable")
};

It would be cleaner (and more typesafe) if the following was possible:

var s = await response.Content.ReadAsStringAsync();                
var hr = JsonSerializer.Deserialize<HealthReport>(s);

But that's not possible due to the lack of a default constructor:
System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Microsoft.Extensions.Diagnostics.HealthChecks.HealthReport'.

Extending the type is also not a workaround, since the class is sealed.

Are we trying to do something that's not intended to do?
Thoughts welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-healthchecksIncludes: Healthchecks (some bugs also in Extensions repo)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions