0

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?

The Inquisitive Coder
  • 1,085
  • 3
  • 20
  • 43
  • I usually don't use Health Checks for Azure Functions based APIs, and the reason is because most of the time I use the consumption plan, and the function won't be running all the time. My way to monitor it is through Application Insights. – Thiago Custodio Jul 06 '23 at 14:59
  • Hey, the requirement in my case is to monitor the health every 5 mins, hence going with this approach. :) – The Inquisitive Coder Jul 06 '23 at 15:01
  • You've setup the healthcheck in DI, but you haven't created a function that uses it: https://stackoverflow.com/questions/75919014/configuring-net-6-azure-function-app-healthchecks/75920076 – Phil S Jul 06 '23 at 15:40
  • @https://stackoverflow.com/questions/75919014/configuring-net-6-azure-function-app-healthchecks/75920076 yes, indeed, I was testing without creating a function that was using the dependency. Thank you so much! – The Inquisitive Coder Jul 06 '23 at 16:17

0 Answers0