diff --git a/src/App.Metrics.Health.Extensions.DependencyInjection/Internal/DefaultHealthCheckTypeProvider.cs b/src/App.Metrics.Health.Extensions.DependencyInjection/Internal/DefaultHealthCheckTypeProvider.cs index eebc032..5012101 100644 --- a/src/App.Metrics.Health.Extensions.DependencyInjection/Internal/DefaultHealthCheckTypeProvider.cs +++ b/src/App.Metrics.Health.Extensions.DependencyInjection/Internal/DefaultHealthCheckTypeProvider.cs @@ -70,8 +70,7 @@ internal static bool IsHealthCheck( return false; } - if (!typeInfo.Name.EndsWith(HealthCheckTypeName, StringComparison.OrdinalIgnoreCase) && - !DerivesFromHealthCheck(typeInfo, candidateAssemblies)) + if (!DerivesFromHealthCheck(typeInfo, candidateAssemblies)) { return false; } diff --git a/test/Health.Extensions.DependencyInjection.Facts/ServiceCollectionHealthBuilderExtensionsTests.cs b/test/Health.Extensions.DependencyInjection.Facts/ServiceCollectionHealthBuilderExtensionsTests.cs index 811f78a..22f4951 100644 --- a/test/Health.Extensions.DependencyInjection.Facts/ServiceCollectionHealthBuilderExtensionsTests.cs +++ b/test/Health.Extensions.DependencyInjection.Facts/ServiceCollectionHealthBuilderExtensionsTests.cs @@ -80,6 +80,24 @@ public void Should_scan_assembly_and_add_health_checks_and_ignore_obsolete_check health.Checks.Skip(1).First().Name.Should().Be("Referenced Health Check"); } + [Fact] + public void Should_only_register_class_deriving_health_check() + { + // Arrange + var services = new ServiceCollection(); + services.AddSingleton(); + + // Act + var unused = new HealthBuilder() + .HealthChecks.RegisterFromAssembly(services, _fixture.DependencyContext) + .BuildAndAddTo(services); + var provider = services.BuildServiceProvider(); + var health = provider.GetRequiredService(); + + // Assert + health.Checks.FirstOrDefault(h => h.Name == nameof(DontRegisterHealthCheck)).Should().BeNull(); + } + [Fact] public void Should_append_resolved_health_checks_to_those_explicitly_registered() { diff --git a/test/Health.Extensions.DependencyInjection.Facts/TestHelpers/DontRegisterHealthCheck.cs b/test/Health.Extensions.DependencyInjection.Facts/TestHelpers/DontRegisterHealthCheck.cs new file mode 100644 index 0000000..d8f1447 --- /dev/null +++ b/test/Health.Extensions.DependencyInjection.Facts/TestHelpers/DontRegisterHealthCheck.cs @@ -0,0 +1,29 @@ +// +// Copyright (c) Allan Hardy. All rights reserved. +// + +using System; +using System.Threading; +using System.Threading.Tasks; +using App.Metrics.Health; + +namespace Health.Extensions.DependencyInjection.Facts.TestHelpers +{ + public class DontRegisterHealthCheck + { + public ValueTask CheckAsync(CancellationToken cancellationToken = default) + { + if (DateTime.UtcNow.Second <= 20) + { + return new ValueTask(HealthCheckResult.Degraded()); + } + + if (DateTime.UtcNow.Second >= 40) + { + return new ValueTask(HealthCheckResult.Unhealthy()); + } + + return new ValueTask(HealthCheckResult.Healthy()); + } + } +}