Skip to content
Closed
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
Next Next commit
Add support for enumerating keyed services with KeyedService.AnyKey
  • Loading branch information
benjaminpetit authored and github-actions committed Dec 11, 2023
commit b9335c2d5e7d808178ffab52752efcfa2202edfe
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,61 @@ public void ResolveKeyedServices()
Assert.Equal(new[] { service2, service3, service4 }, services);
}

[Fact]
public void ResolveKeyedServicesAnyKey()
{
var service1 = new Service();
var service2 = new Service();
var service3 = new Service();
var service4 = new Service();
var service5 = new Service();
var service6 = new Service();
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedSingleton<IService>("first-service", service1);
serviceCollection.AddKeyedSingleton<IService>("service", service2);
serviceCollection.AddKeyedSingleton<IService>("service", service3);
serviceCollection.AddKeyedSingleton<IService>("service", service4);
serviceCollection.AddKeyedSingleton<IService>(null, service5);
serviceCollection.AddSingleton<IService>(service6);

var provider = CreateServiceProvider(serviceCollection);


// Return all services registered with a non null key
var allServices = provider.GetKeyedServices<IService>(KeyedService.AnyKey).ToList();
Assert.Equal(4, allServices.Count);
Assert.Equal(new[] { service1, service2, service3, service4 }, allServices);
}

[Fact]
public void ResolveKeyedServicesAnyKeyWithAnyKeyRegistration()
{
var service1 = new Service();
var service2 = new Service();
var service3 = new Service();
var service4 = new Service();
var service5 = new Service();
var service6 = new Service();
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedTransient<IService>(KeyedService.AnyKey, (sp, key) => new Service());
serviceCollection.AddKeyedSingleton<IService>("first-service", service1);
serviceCollection.AddKeyedSingleton<IService>("service", service2);
serviceCollection.AddKeyedSingleton<IService>("service", service3);
serviceCollection.AddKeyedSingleton<IService>("service", service4);
serviceCollection.AddKeyedSingleton<IService>(null, service5);
serviceCollection.AddSingleton<IService>(service6);

var provider = CreateServiceProvider(serviceCollection);

_ = provider.GetKeyedService<IService>("something-else");
_ = provider.GetKeyedService<IService>("something-else-again");

// Return all services registered with a non null key, but not the one "created" with KeyedService.AnyKey
var allServices = provider.GetKeyedServices<IService>(KeyedService.AnyKey).ToList();
Assert.Equal(5, allServices.Count);
Assert.Equal(new[] { service1, service2, service3, service4 }, allServices.Skip(1));
}

[Fact]
public void ResolveKeyedGenericServices()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ private static bool AreCompatible(DynamicallyAccessedMemberTypes serviceDynamica
ServiceCallSite[] callSites;

// If item type is not generic we can safely use descriptor cache
// Special case for KeyedService.AnyKey, we don't want to check the cache because a KeyedService.AnyKey registration
// will "hide" all the other service registration
if (!itemType.IsConstructedGenericType &&
!KeyedService.AnyKey.Equals(cacheKey.ServiceKey) &&
_descriptorLookup.TryGetValue(cacheKey, out ServiceDescriptorCacheItem descriptors))
{
callSites = new ServiceCallSite[descriptors.Count];
Expand Down Expand Up @@ -694,7 +697,11 @@ private static bool KeysMatch(object? key1, object? key2)
return true;

if (key1 != null && key2 != null)
return key1.Equals(KeyedService.AnyKey) || key1.Equals(key2);
{
return key1.Equals(key2)
|| key1.Equals(KeyedService.AnyKey)
|| key2.Equals(KeyedService.AnyKey);
}

return false;
}
Expand Down