-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Microsoft DI resolves enumerables incorrectly when a matching ServiceType has already been resolved #87017
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsDescriptionWhen a user registers multiple services with matching a
Reproduction StepsI found this bug while investigating an issue with MediatR, and followed the (very) long debugging path to Expected behaviorWhen calling Actual behavior
Regression?I believe that this issue was introduced by the changes made for #52035 as the root cause of the issue is the use of the Known WorkaroundsNone. Configuration
Other informationI have already written a fix for this issue and will create a PR/draft PR for it once I clean up my branch a bit, but would greatly appreciate any suggestions on the changes made.
|
We now attempt to match each descriptor against both the service type and implementation type of cached call sites to account for slot mismatches Added unit test to validate this functionality Fix dotnet#87017
PR has been created here with a fix. |
Thank god, this bug was so annoying. |
Thoughts @davidfowl? |
Yikes, good find! EDIT: @mesakomarevich can you verify if #80410 fixes your issue? I'm wondering if this is the same bug. |
@davidfowl Looking into this now |
@davidfowl after merging the tests added in each branch and running them against my changes and the changes in #80410, I can confirm that both branches pass all tests, and that #80410 fixes my issue. So it seems that either solution should work. There may be some performance differences between our changes, but by squinting real hard at the code, I don't imagine that they would be very significant. |
Are there any requests to backport the fix to v7 and possible v6 (since it's LTS)? |
I think we should backport once we're confident in the fix. We may want to wait for this change to exist in .NET 8 for a bit before we do that. |
@davidfowl could we possibly release a beta version of a backport to v6 and v7? As per this issue raised in MediatR, there are a couple of us who have run into this issue in production code and are looking for a solution. |
We don’t do beta versions of patches. We’ll back port the fix for sure. It would be great if you could use the 8.0 preview of this package to help verify it (once it comes out). |
Description
When a user registers multiple services with matching a
serviceType
in aServiceCollection
and tries to resolve them with theGetServices<T>
extension method, they will receiveIEnumerable<T>
of the correct length but with some services duplicated, and others missing. This will occur if the following conditions are met:T
have been previously resolved and cachedT
do not haveServiceCacheKey.Slot
values that match their inverse order of appearance in the_descriptors
array inCallSiteFactory
Reproduction Steps
I found this bug while investigating an issue with MediatR, and followed the (very) long debugging path to
CallSiteFactory
by using the minimal reproduction provided by @MichaelHochriegl for the MediatR issue here.Expected behavior
When calling
GetServices<T>
, all services registered withT
as their service type are returned, regardless of which service(s)T
have been resolved earlier in the program.Actual behavior
GetServices<T>
returns an erroneous list of services of the expected length, but with some services being duplicated and others missing.Regression?
I believe that this issue was introduced by the changes made for #52035 as the root cause of the issue is the use of the
_callSiteCache
to optimize service lookups.Known Workarounds
None.
Configuration
Microsoft.Extensions.DependencyInjection.Abstractions
nuget package.Other information
I have already written a fix for this issue and will create a PR/draft PR for it once I clean up my branch a bit, but would greatly appreciate any suggestions on the changes made. Feel free to assign this issue to me.
The text was updated successfully, but these errors were encountered: