.netazure-functions.net-5health-checkazure-http-trigger

Adding Health Check to .NET Isolated Azure Function


I could not find any resources to add health checks to a HTTPTrigger function app, running in .NET 5.0 Isolated.

static async Task Main()
{
    var host = new HostBuilder()
        .ConfigureAppConfiguration(configurationBuilder =>
        {
            configurationBuilder.AddEnvironmentVariables();
        })
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices((builder, services) =>
        {
            var configuration = builder.Configuration;
            services.AddDbContext(configuration);
                    
            // Add healthcheck here
            services.AddHealthChecks()
            // ...
            // Map health checks
                    
        })
        .Build();

    await host.RunAsync();
}

This guide states I can add MapHealthChecks (but in asp.net core)

var app = builder.Build();

app.MapHealthChecks("/healthz");

app.Run();

How do I translate this to run in my dotnet-isolated application?


Solution

  • app.MapHealthChecks("/healthz");
    

    The line above under the hood creates ASP CORE middleware and injects IHealthCheckService to call CheckHealthAsync() and return HTTP response. In the Azure Function you can: