-
Notifications
You must be signed in to change notification settings - Fork 753
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
AddStandardResilienceHandler messes up DI container, requiring strict ordering which is bad practice #5222
Comments
I also ran into this issue. The workaround does work, but it is unfortunate that order matters in DI. |
Thanks for the bug report @sander1095, this is indeed interesting and we should take a peek. By just skimming the code I'm not sure what may be going on here as the standard resilience handler should only be adding services to the container and not removing any. cc @iliar-turdushev this might require some debugging to figure out. |
ApplicationInsights has a ApplicationInsights is generally designed as best effort observability, which means that if it encounters errors it is not supposed to take down your service, and this is taken to the point where it mostly silently catches exceptions that occur inside (This also means that doing All that said, this is fundamentally just a bug with Application Insight's |
Thanks for the info @kevincathcart-cas, I actually didn't know that so today I learned 😃. I was able to confirm this by swapping the call to AddStandardResilienceHandler in your repro to a simple |
@sander1095 FWIW, in case you plan to open an issue this is the method in question: |
Thanks! I had already opened an issue but it needs to be updated with these (fascinating) details! |
Description
Using
AddStandardResilienceHandler()
does something with the service collection, causing Application Insights'TelemtryClient
to go missing from the DI container.I'm pretty sure (or I hope that) this bug is not related to Application Insights; most likely it has something to do with the way this package changes the ServiceCollection. However, I will demonstrate it using Application Insights because that's how I bumped into this issue.
Reproduction Steps
Microsoft.ApplicationInsights.WorkerService
Microsoft.Extensions.Http.Resilience
Write the following in Program.cs:
Expected behavior
I would expect that IServiceCollection does NOT require the caller to configure it in a specific order. This is considered a bad practice; the http pipeline in ASP.NET Core is ordered, for example ,but the DI is not.
Actual behavior
Using
AddStandardResilienceHandler()
modifies the service collection somehow, causing some services to become unresolvable.Regression?
No response
Known Workarounds
Configure Resilience last in your service collection
Configuration
.NET 8
Windows 11
Other information
The docs mention the following:
Which changed my mind on guessing this was a
resilience <-> app insights
issue, and more likely related to how this resilience package modifies the service collection in an illegal/bad practice kind of way.https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
The text was updated successfully, but these errors were encountered: