-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix consistency issue in GetKeyedServices with AnyKey #97561
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||
using System.Security.Cryptography; | ||||||||
using Microsoft.Extensions.DependencyInjection.Specification.Fakes; | ||||||||
using Xunit; | ||||||||
using static Microsoft.Extensions.DependencyInjection.Specification.KeyedDependencyInjectionSpecificationTests; | ||||||||
|
||||||||
namespace Microsoft.Extensions.DependencyInjection.Specification | ||||||||
{ | ||||||||
|
@@ -152,10 +153,48 @@ public void ResolveKeyedServicesAnyKeyWithAnyKeyRegistration() | |||||||
_ = 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 | ||||||||
// Return all services registered with a non null key, but not the one "created" with KeyedService.AnyKey, | ||||||||
// nor the KeyedService.AnyKey registration | ||||||||
var allServices = provider.GetKeyedServices<IService>(KeyedService.AnyKey).ToList(); | ||||||||
Assert.Equal(5, allServices.Count); | ||||||||
Assert.Equal(new[] { service1, service2, service3, service4 }, allServices.Skip(1)); | ||||||||
Assert.Equal(4, allServices.Count); | ||||||||
Assert.Equal(new[] { service1, service2, service3, service4 }, allServices); | ||||||||
} | ||||||||
|
||||||||
[Fact] | ||||||||
public void ResolveKeyedServicesAnyKeyConsistency() | ||||||||
{ | ||||||||
var serviceCollection = new ServiceCollection(); | ||||||||
var service = new Service("first-service"); | ||||||||
serviceCollection.AddKeyedSingleton<IService>("first-service", service); | ||||||||
|
||||||||
var provider1 = CreateServiceProvider(serviceCollection); | ||||||||
Assert.Null(provider1.GetKeyedService<IService>(KeyedService.AnyKey)); | ||||||||
// We don't return KeyedService.AnyKey registration when listing services | ||||||||
Assert.Equal(new[] { service }, provider1.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
|
||||||||
var provider2 = CreateServiceProvider(serviceCollection); | ||||||||
benjaminpetit marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Assert.Equal(new[] { service }, provider2.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
// But we should be able to directly do a lookup on it | ||||||||
Assert.Null(provider2.GetKeyedService<IService>(KeyedService.AnyKey)); | ||||||||
} | ||||||||
|
||||||||
[Fact] | ||||||||
public void ResolveKeyedServicesAnyKeyConsistencyWithAnyKeyRegistration() | ||||||||
{ | ||||||||
var serviceCollection = new ServiceCollection(); | ||||||||
var service = new Service("first-service"); | ||||||||
var any = new Service("any"); | ||||||||
serviceCollection.AddKeyedSingleton<IService>("first-service", service); | ||||||||
serviceCollection.AddKeyedSingleton<IService>(KeyedService.AnyKey, (sp, key) => any); | ||||||||
|
||||||||
var provider1 = CreateServiceProvider(serviceCollection); | ||||||||
Assert.Same(any, provider1.GetKeyedService<IService>(KeyedService.AnyKey)); | ||||||||
Assert.Equal(new[] { service }, provider1.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
|
||||||||
// Check twice in different order to check caching | ||||||||
var provider2 = CreateServiceProvider(serviceCollection); | ||||||||
Assert.Equal(new[] { service }, provider2.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
Assert.Same(any, provider2.GetKeyedService<IService>(KeyedService.AnyKey)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that the If you have
Given this, and that you can just use Technically this is breaking like any behavior change is, but I would be surprised if anything other than our tests specifically try to resolve a single service with the
Suggested change
Less importantly, it also leaves the door open for us retconning what the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we should throw on |
||||||||
} | ||||||||
|
||||||||
[Fact] | ||||||||
|
@@ -243,7 +282,7 @@ public void ResolveKeyedServicesSingletonInstanceWithAnyKey() | |||||||
var provider = CreateServiceProvider(serviceCollection); | ||||||||
|
||||||||
var services = provider.GetKeyedServices<IFakeOpenGenericService<PocoClass>>("some-key").ToList(); | ||||||||
Assert.Equal(new[] { service1, service2 }, services); | ||||||||
Assert.Equal(new[] { service2 }, services); | ||||||||
} | ||||||||
|
||||||||
[Fact] | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'm assuming this all works, and some of this might even be covered partially by other tests. But we don't have many tests with the combination of
AnyKey
registrations, unkeyed registrations, and enumerable resolution of both keyed and unkeyed services. And I don't see the harm in extra verification.