2

I'm struggling to find any up to date documentation on how to setup healthchecks in Azure for a .NET 6 Core function app.

The app is running in DOTNET-ISOLATED, within an app service plan (not consumption).

This documentation explains how to setup the health check part in Azure (easy enough).

But it doesn't explain anywhere, code-wise, what needs to run on the function app side. There's all sorts of health check stuff available, but it all seems to be geared towards WebApi, or there are no examples for .NET 6.

It seems daft that this is so easy to do (with a couple of one-liners) for WebApi, but not for their own function apps?

This looks simple, but appears to only work for consumption plans?

I've looked at a few articles and answers, including this one, for .NET 5, but it lacks detail and doesn't seem like an obvious translation to .NET 6?

Does anyone have an example of some working code for this? How indepth does the healthcheck need to be? Can I just write a simple function that returns 200 and point Azure to it?

Phil S
  • 642
  • 8
  • 19

1 Answers1

3

After a fair amount of mucking around, I eventually came up with this, which seems to be doing the job.

First of all, install nuget package:

Microsoft.Extensions.Diagnostics.HealthChecks
Version 6.0.15

Add this to your Startup / HostBuilder

services.AddHealthChecks();

Then add a function something like this:

using Microsoft.AspNetCore.Routing;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Grpc.Core;

namespace MyFunctionAppNameSpace
{
    public class HealthCheck
    {
        private readonly HealthCheckService _healthCheck;

        public HealthCheck(HealthCheckService healthCheck)
        {
            _healthCheck = healthCheck;
        }

        [Function(nameof(HealthCheck))]
        public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "health")] HttpRequestData req,
            FunctionContext context)
        {
            var healthStatus = await _healthCheck.CheckHealthAsync();
            return new OkObjectResult(Enum.GetName(typeof(HealthStatus), healthStatus.Status));
        }
    }
}

Finally, in Azure, setup the health check (after deploying your code, multiple times if you use slots):

Azure Health Check Setup

Hopefully this saves somebody else the hassle...

Phil S
  • 642
  • 8
  • 19
  • Hello @https://stackoverflow.com/users/7861206/phil-s , I'm facing a similar issue, can you please have a look and advice - https://stackoverflow.com/questions/76629891/how-to-configure-health-check-for-azure-function-app – The Inquisitive Coder Jul 06 '23 at 14:59