-
Notifications
You must be signed in to change notification settings - Fork 841
Description
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>>().