Describe the bug
Maybe I'm wrong, but I think that in this line
https://github.com/aspnet/Extensions/blob/112c1a8ae1c0a921eae4490abd4ccdfcdb899574/src/HealthChecks/HealthChecks/src/DependencyInjection/HealthCheckServiceCollectionExtensions.cs#L29
there must be AddSingleton method instead of TryAddSingleton method, because TryAddSingleton will check only ServiceType property:
https://github.com/aspnet/Extensions/blob/3ba072c25373c19ab2cfe34483e733f3b926b2c0/src/DependencyInjection/DI.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs#L88
So, if we have multiple hosted services (one is enough) which were added to IServiceCollection
(probably through AddHostedService extension method) before AddHealthChecks call , then the TryAddSingleton won`t do anything.
Also, as I see in comments, AddHealthChecks method is supposed to be idempotent, so maybe it is better to use TryAddEnumerable method explicitly? https://github.com/aspnet/Extensions/blob/3ba072c25373c19ab2cfe34483e733f3b926b2c0/src/DependencyInjection/DI.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs#L585
TryAddEnumerable will check both ServiceType and ImplementationType properties, so HealthCheckPublisherHostedService ultimately will be added to IServiceCollection and Host will be able to resolve it through Services.GetService<IEnumerable<IHostedService>>().
Describe the bug
Maybe I'm wrong, but I think that in this line
https://github.com/aspnet/Extensions/blob/112c1a8ae1c0a921eae4490abd4ccdfcdb899574/src/HealthChecks/HealthChecks/src/DependencyInjection/HealthCheckServiceCollectionExtensions.cs#L29
there must be
AddSingletonmethod instead ofTryAddSingletonmethod, becauseTryAddSingletonwill check onlyServiceTypeproperty:https://github.com/aspnet/Extensions/blob/3ba072c25373c19ab2cfe34483e733f3b926b2c0/src/DependencyInjection/DI.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs#L88
So, if we have multiple hosted services (one is enough) which were added to
IServiceCollection(probably through
AddHostedServiceextension method) beforeAddHealthCheckscall , then theTryAddSingletonwon`t do anything.Also, as I see in comments,
AddHealthChecksmethod is supposed to be idempotent, so maybe it is better to useTryAddEnumerablemethod explicitly? https://github.com/aspnet/Extensions/blob/3ba072c25373c19ab2cfe34483e733f3b926b2c0/src/DependencyInjection/DI.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.cs#L585TryAddEnumerablewill check bothServiceTypeandImplementationTypeproperties, soHealthCheckPublisherHostedServiceultimately will be added toIServiceCollectionandHostwill be able to resolve it throughServices.GetService<IEnumerable<IHostedService>>().