-
Notifications
You must be signed in to change notification settings - Fork 5.3k
When parameter type has [ServiceKey] allow 'object' as the parameter type #114785
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
Conversation
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.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
| // Assert | ||
| Assert.Equal(42, actualInt.Key); | ||
| Assert.Equal("hello", actualString.Key); | ||
| Assert.Null(notFound); |
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.
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.
The bool is not registered with AddKeyedTransient(); only int and string are.
| // Act | ||
| var actualInt = serviceProvider.GetKeyedService<ServiceKeyWithObjectTypedArgument>(42); | ||
| var actualString = serviceProvider.GetKeyedService<ServiceKeyWithObjectTypedArgument>("hello"); | ||
| var notFound = serviceProvider.GetKeyedService<ServiceKeyWithObjectTypedArgument>(false); |
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.
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.
Passing null means that this is not a "keyed service" so it just uses the type which is ServiceKeyWithObjectTypedArgument and since that was not registered, it returns null.
These checks are really just verifying that [ServiceKey] object key is called with the correct key and no unexpected exceptions occur.
tarekgh
left a comment
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.
LGTM. can you think of any breaking can happen because of this change other than allowing the object scenario that used to throw before?
No breaking changes that I can think of. This functionality is just injecting the key used, no lookups or anything, so it's fairly isolated. |
When the
[ServiceKey]attribute is applied to a constructor parameter (which sets the parameter value to the key that was used to resolve the service), support usingobjectas the parameter type.Before this PR, the parameter had to be the actual type the service is requested with, even when
AnyKeyis used. With this PR, the parameter can now also beobjectfor both cases where the service is registered as the actual type or registered asAnyKey.Supporting both cases helps decouple the service implementation from the registration key type when the service implementation elects to use
objectas the parameter type with[ServiceKey].Also fixed was a validation issue when
AnyKeywas used withValidateOnBuild=truewhich caused[ServiceKey]not to work at all even if the parameter type was the actual type.Fixes #114780
Fixes #93438