Description
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.