Files
MCVIngenieros.Healthchecks/HealthCheckServiceCollectionExtensions.cs
2025-08-25 23:46:04 +02:00

86 lines
3.5 KiB
C#

using MCVIngenieros.Healthchecks.__;
using MCVIngenieros.Healthchecks.__.__Controllers;
using MCVIngenieros.Healthchecks.Abstracts;
using MCVIngenieros.Healthchecks.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Reflection;
namespace MCVIngenieros.Healthchecks;
// Extension methods for IServiceCollection
public static class HealthCheckServiceCollectionExtensions
{
private static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, HealthCheckContainerOptions options)
{
ArgumentNullException.ThrowIfNull(services, nameof(services));
services.AddSingleton<IHealthCheckContainer, HealthCheckContainer>();
services.AddSingleton(Microsoft.Extensions.Options.Options.Create(options));
services.AddCheck<ImAliveHealthcheck>();
services.AddControllers().AddApplicationPart(typeof(HealthCheckController).Assembly);
return services;
}
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services, nameof(services));
var options = new HealthCheckContainerOptions();
var scope = services.BuildServiceProvider().CreateScope();
var config = scope.ServiceProvider.GetRequiredService<IConfiguration>();
scope.Dispose();
services.Configure<HealthCheckOptions>(nameof(HealthCheckOptions), config.GetSection(nameof(HealthChecksConfigs)));
scope = services.BuildServiceProvider().CreateScope();
var hcopts = scope.ServiceProvider
.GetRequiredService<IOptionsSnapshot<HealthCheckOptions>>().Get(nameof(HealthCheckOptions));
scope.Dispose();
if (!string.IsNullOrWhiteSpace(hcopts.CacheDuration) && TimeSpan.TryParse(hcopts.CacheDuration, out TimeSpan cacheDuration))
{
options.CacheDuration = cacheDuration;
}
if (!string.IsNullOrWhiteSpace(hcopts.Timeout) && TimeSpan.TryParse(hcopts.Timeout, out TimeSpan timeout))
{
options.Timeout = timeout;
}
if (hcopts.AssembliesToScan != null && hcopts.AssembliesToScan.Length > 0)
{
options.AssembliesToScan = hcopts.AssembliesToScan.Select(Assembly.Load);
}
return services.AddHealthChecksSupport(options);
}
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, Action<HealthCheckContainerOptions>? configure = null)
{
ArgumentNullException.ThrowIfNull(services, nameof(services));
ArgumentNullException.ThrowIfNull(configure, nameof(configure));
var options = new HealthCheckContainerOptions();
configure?.Invoke(options);
return services.AddHealthChecksSupport(options);
}
public static IServiceCollection AddHealthCheck<T>(this IServiceCollection services)
where T : class, IHealthCheck
=> services.AddCheck<T>();
public static IServiceCollection AddCheck<T>(this IServiceCollection services)
where T : class, IHealthCheck
{
services.TryAddTransient<IHealthCheck, T>();
return services;
}
public static IServiceCollection DiscoverHealthChecks(this IServiceCollection services)
{
// This will trigger the discovery when the container is built
var provider = services.BuildServiceProvider();
var container = provider.GetRequiredService<IHealthCheckContainer>();
container.DiscoverHealthChecks();
return services;
}
}