// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
namespace Aspire;
internal static class HealthChecksExtensions
{
///
/// Adds a HealthCheckRegistration if one hasn't already been added to the builder.
///
public static void TryAddHealthCheck(this IHostApplicationBuilder builder, HealthCheckRegistration healthCheckRegistration)
{
builder.TryAddHealthCheck(healthCheckRegistration.Name, hcBuilder => hcBuilder.Add(healthCheckRegistration));
}
///
/// Invokes the action if the given hasn't already been added to the builder.
///
public static void TryAddHealthCheck(this IHostApplicationBuilder builder, string name, Action addHealthCheck)
{
var healthCheckKey = $"Aspire.HealthChecks.{name}";
if (!builder.Properties.ContainsKey(healthCheckKey))
{
builder.Properties[healthCheckKey] = true;
addHealthCheck(builder.Services.AddHealthChecks());
}
}
}