Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryAddSingleton possible misuse in AddHealthChecks method #639

Closed
sungam3r opened this issue Dec 12, 2018 · 7 comments
Closed

TryAddSingleton possible misuse in AddHealthChecks method #639

sungam3r opened this issue Dec 12, 2018 · 7 comments
Assignees
Milestone

Comments

@sungam3r
Copy link

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>>().

@rynowak
Copy link
Member

rynowak commented Dec 12, 2018

Yes, you are right. This is a bug!

@sungam3r
Copy link
Author

Will this be fixed in the next release?

@rynowak rynowak self-assigned this Dec 24, 2018
@rynowak rynowak added this to the 3.0.0 milestone Dec 24, 2018
@rynowak
Copy link
Member

rynowak commented Dec 24, 2018

This will be fixed in 3.0 - is this blocking for you in 2.2? Do you need help finding a workaround?

rynowak added a commit that referenced this issue Dec 24, 2018
The issue is that the hosted service would not be registered if other
hosted services are present. The fix is to use TryAddEnumble so that we
allow multipled `IHostedService`s but not multiple instances of
the same type.
@sungam3r
Copy link
Author

No, now it is not critical. Just after reading the release notes about health checks in netcore 2.2, looking through the code found it. Thanks!

@espenrl
Copy link

espenrl commented Jan 23, 2019

Can this be fixed in a patch release of 2.2? We have several IHostedService services and thus the HealthCheckPublisherHostedService is never registered with the container. I can't do the registration myself because HealthCheckPublisherHostedService is an internal class. End result is that we can't use the publisher feature. A long time to 3.0...

EDIT: I moved the registrations of our custom IHostedService until after AddHealthChecks(). Then it worked.

@NatMarchand
Copy link
Contributor

For people stuck, one possible workaround is:

services.AddHealthChecks();

// This is a hack to fix an issue with the AddApplicationInsightsPublisher() call above
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), typeof(HealthCheckPublisherOptions).Assembly.GetType("Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckPublisherHostedService")));

@sungam3r
Copy link
Author

@NatMarchand I see your workaround in docs but it is written incorrectly. HealthCheckServiceAssembly should be changed to "Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckPublisherHostedService".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants