Repository navigation
Fixes incorrect service resolution in Development environment - #68053
maryamariyan wants to merge 3 commits into
Conversation
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue Details
PR that added support for constrained open generics: #39540 cc @jbogard
|
- Fixed ValidateOnBuild to properly resolve constrained open generics
|
|
||
| private void ValidateService(ServiceDescriptor descriptor) | ||
| { | ||
| if (descriptor.ServiceType.IsGenericType && !descriptor.ServiceType.IsConstructedGenericType) |
There was a problem hiding this comment.
@maryamariyan could you explain more about this change?
From the code I think when having IsConstructedGenericType should try to Get or create the callsite? for IEnumerable<>, maybe need to check for interfaces instead?
There was a problem hiding this comment.
Adding a comment to the code might be a good idea.
There was a problem hiding this comment.
What I noticed is if the type is generic (constrained or not) ValidateService would incorrectly resolve the generics.
Also since we already have the test below expecting ValidateOnBuild to be skipped for generics, then I'm more convinced this is the fix we want for the reported bug.
Added comment:
// Skip validation for generic service typesThere was a problem hiding this comment.
This is a breaking change right? Now closed generics won't be validated?
|
Here's some more info for review:
what would work is to either not add cache callsites at all during validate, or place them at the right slot as we add them. To mitigate this, another approach I tried was to replace the already inserted cache if the implementation type does not match the descriptor implementation type. but this approach would fail for singletons. (Refer to commit 5dde8ce). I will look to see if we can do better. |
|
I still dont think the existing PR I have is incorrect per say, the downside is that we no longer validate for generics. I think my current fix should still be fine. this caching bug would only happen when we are validating on build with open generics available in descriptor list. the fact that with validate we are skipping some descriptors (the open generic ones) means that if we are in fact later adding the rest in the callSiteCache the ORDER will become messed up for the ones we add later (and not coherent with the descriptor list). and if we are to validate but not add to cache, then for singletons we would be loosing the singleton instance if we don't add to cache. thoughts? maybe a midway compromise is to allow validate generics to happen only for transient ones, but I wouldnt like special casing the validation scenario this way. |
Is this acceptable behavior? Why is it OK to no longer validate constructed generic services? |
I think when we also have open generics registered, validating constructed generics will mess with the slot ordering, unless we had a separate cache for open generics to identify slots on but might be a bigger fix than we want |
eerhardt
left a comment
There was a problem hiding this comment.
I think we've been through all the possible solutions to this problem, and skipping validation seems to be the only viable solution.
I'm OK with moving forward with this change to get the scenario working correctly. @davidfowl - any concerns?
| private void ValidateService(ServiceDescriptor descriptor) | ||
| { | ||
| if (descriptor.ServiceType.IsGenericType && !descriptor.ServiceType.IsConstructedGenericType) | ||
| // Skip validation for generic service types |
There was a problem hiding this comment.
We should add more description here explaining why we are skipping validation for generic service types.
There was a problem hiding this comment.
add a commit to address this
davidfowl
left a comment
There was a problem hiding this comment.
I need to do one more pass.
|
This pull request has been automatically marked |
|
Reviewed this PR with @davidfowl late last week and here's a summary to it:
-ServiceCallSite? callSite = CallSiteFactory.GetCallSite(descriptor, new CallSiteChain());
+ServiceCallSite? callSite = CallSiteFactory.GetCallSite(typeof(IEnumerable<>).MakeGenericType(descriptor.ServiceType), new CallSiteChain());
|
Today, if the service type is generic (constrained or not)
ValidateServicewould cause a bug and incorrectly resolve the generics.Debug notes
Given:
When
BuildServiceProvidergets called for:ValidateOnBuildmakesCallSiteFactorystore some service call sites into_callSiteCache.Currently because of the if condition:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs
Lines 133 to 142 in 2056321
for the first transient descriptor a cache item gets created for
IBB<AA>in_callSiteCache, but skips to add another cache item forIBB<>to with implementation typeConstrainedGenericBB.Then when we call
DI thinks it already has all the cache info it needs to resolve this and resolves twice with the one cache item rather than also resolving with
ConstrainedGenericBB.Proposed fix
I think the expectation and fix here should be to skip
ValidateOnBuildon generics. We already have the test below expectingValidateOnBuildto be skipped for generics anyway, so that makes me more convinced that this is the fix we want for the reported bug:runtime/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderValidationTests.cs
Lines 137 to 146 in 8ea879a
When we recently added more support for generic service resolution (open generics), we missed out on testing it with
ValidateOnBuild. PR that added support for constrained open generics: #39540This bug fix helps properly resolve
IEnumerable<>services set withValidateOnBuildusingGetServiceswhen dealing with constrained open generics.Fixes #65145
cc @jbogard