I didn't come across any source with the correct answer(I have tried 2 approaches but none of them seem to work), hence posting my question here.
I am using a time trigger Azure Function to check health of other endpoints(which would be read from configuration). For that I'm using the package - Microsoft.Extensions.Diagnostics.HealthChecks
.
# Approach 1
Startup.cs
class:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(HealthChecker.Startup))]
namespace HealthChecker
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Getting the IConfiguration instance
var configuration = builder.GetContext().Configuration;
// Registering health checks
builder.Services.AddHealthChecks();
// Calling the method that defines health checks
HealthCheckDefinitions.ConfigureHealthChecks(builder.Services.AddHealthChecks(), configuration);
}
}
}
HealthCheckDefinitions.cs
class:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
namespace HealthChecker
{
public static class HealthCheckDefinitions
{
public static void ConfigureHealthChecks(IHealthChecksBuilder builder, IConfiguration configuration)
{
try
{
builder.AddCheck("test", () =>
{
bool isHealthy = false;
// Perform a test to check the connectivity to an endpoint
// .....
if (isHealthy)
return HealthCheckResult.Healthy("API connection is healthy.");
else
return HealthCheckResult.Unhealthy("API connection is unhealthy.");
});
}
catch(Exception ex)
{
throw ex;
}
}
}
}
The issue here is the control never steps intobuilder.AddCheck()
although the ConfigureHealthChecks
method gets called.
# Approach 2
I followed the official documentation and tried like this:
Startup.cs
class:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(HealthChecker.Startup))]
namespace HealthChecker
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Get the IConfiguration instance
var configuration = builder.GetContext().Configuration;
// Register health checks
builder.Services.AddHealthChecks().AddCheck<SampleHealthCheck>("SampleHealthCheckName"); ;
}
}
}
SampleHealthCheck.cs
class:
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Threading;
using System.Threading.Tasks;
namespace HealthChecker
{
public class SampleHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default)
{
var isHealthy = true;
// ...
if (isHealthy)
{
return Task.FromResult(
HealthCheckResult.Healthy("A healthy result."));
}
return Task.FromResult(
new HealthCheckResult(
context.Registration.FailureStatus, "An unhealthy result."));
}
}
}
The same issue persists here also. The control never steps into CheckHealthAsync()
of SampleHealthCheck.cs
class.
What am I doing wrong?