Only close open generic registrations once per closed service type - #617
Merged
Merged
Conversation
With Microsoft compatibility enabled, every enumerable request for the same closed generic service type closed the open generic registrations again and appended them to the list of emitters, resulting in duplicate items (e.g. IEnumerable<IBehavior<int>> returning 4 items instead of 2). The same happened when a closed generic had already been registered through a single service request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #616 (found while investigating #614).
Problem
With
EnableMicrosoftCompatibility,CreateEmitMethodForEnumerableServiceServiceRequestcloses all open generic registrations and appends them toallEmittersevery time an enumerable emit method is created for a closed generic service type. Emit methods are created more than once for the same type (as a dependency, as a root service, viaCanGetInstance), so items accumulate:Calling
CanGetInstancefirst gives 4 and 6. A closed generic that was first registered through a single-service request (CreateEmitMethodBasedOnClosedGenericServiceRequest) was also added again by the enumerable path. For MediatR users this means pipeline behaviors can run multiple times.Without Microsoft compatibility the problem was masked because emitters are stored per service name (
TryAdd).Fix
Track, per closed generic service type, which open generic registrations (identified by their unique
RegistrationOrder) have already been closed, and skip them on subsequent requests. The single-service path marks its registration too, when the closed registration uses the same service name as the open one.Tests
ClosedGenericEnumerableTestscovers resolving the enumerable multiple times, afterCanGetInstance, and after a single-service resolve, with and without Microsoft compatibility. The MS-compat cases fail on master and pass with the fix.🤖 Generated with Claude Code