Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,21 @@ private static bool AreCompatible(DynamicallyAccessedMemberTypes serviceDynamica
// For AnyKey, we want to cache based on descriptor identity, not AnyKey that cacheKey has.
ServiceIdentifier registrationKey = isAnyKeyLookup ? ServiceIdentifier.FromDescriptor(_descriptors[i]) : cacheKey;
slot = GetSlot(registrationKey);
if (CreateOpenGeneric(_descriptors[i], registrationKey, callSiteChain, slot, throwOnConstraintViolation: false) is { } callSite)

// We skip open generics with incompatible constraints.
if (CreateOpenGeneric(_descriptors[i], registrationKey, callSiteChain, slot, false) is { } callSite)
{
AddCallSite(callSite, i);
UpdateSlot(registrationKey);
}
else if (slot == 0)
{
// If the last registration has incompatible constraints, we still need to update the slot.
// This ensures that single service resolution (GetService) will attempt to resolve using the last
// registration and throw an ArgumentException, maintaining "last wins" semantics. During enumerable
// resolution (GetServices), the incompatible registration is simply skipped.
UpdateSlot(registrationKey);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,18 @@ public void ResolveKeyedServiceWithKeyedParameter_MissingRegistrationButWithUnke
Assert.Contains("Microsoft.Extensions.DependencyInjection.Specification.KeyedDependencyInjectionSpecificationTests+IService", ex.ToString());
}

[Fact]
public void InvalidConstrainedOpenGenericIsSkippedInEnumerableButThrowsInSingleResolution()
{
var sc = new ServiceCollection();
sc.AddSingleton(typeof(IBB<>), typeof(GenericBB<>));
sc.AddSingleton(typeof(IBB<>), typeof(ConstrainedGenericBB<>));
var sp = CreateServiceProvider(sc);

Assert.Single(sp.GetServices<IBB<IBB<string>>>());
Assert.Throws<ArgumentException>(() => sp.GetService<IBB<IBB<string>>>());
}

private async Task<bool> ResolveUniqueServicesConcurrently()
{
var types = new Type[]
Expand Down