Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,19 @@ 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 to allow direct resolution to correctly throw later
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 async Task InvalidConstrainedOpenGenericThrowsWhenResolvedAsEnumerable()
{
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
Loading