-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix consistency issue in GetKeyedServices with AnyKey #97561
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
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 | ||||||||
| { | ||||||||
|
|
@@ -158,6 +159,40 @@ public void ResolveKeyedServicesAnyKeyWithAnyKeyRegistration() | |||||||
| Assert.Equal(new[] { service1, service2, service3, service4 }, allServices.Skip(1)); | ||||||||
| } | ||||||||
|
|
||||||||
| [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)); | ||||||||
| Assert.Equal(new[] { service }, provider1.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
|
|
||||||||
| var provider2 = CreateServiceProvider(serviceCollection); | ||||||||
| Assert.Equal(new[] { service }, provider2.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
| 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, any }, provider1.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
|
|
||||||||
| var provider2 = CreateServiceProvider(serviceCollection); | ||||||||
| Assert.Equal(new[] { service, any }, provider2.GetKeyedServices<IService>(KeyedService.AnyKey)); | ||||||||
| Assert.Same(any, provider2.GetKeyedService<IService>(KeyedService.AnyKey)); | ||||||||
|
Member
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
Contributor
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] | ||||||||
| public void ResolveKeyedGenericServices() | ||||||||
| { | ||||||||
|
|
||||||||
Uh oh!
There was an error while loading. Please reload this page.