Skip to content

Improve error message when GetRequiredKeyedService() fails #114796

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

Merged
merged 1 commit into from
Apr 18, 2025

Conversation

steveharter
Copy link
Contributor

Adds "keyed" and the name of the key type to the message to help with diagnostics.

Fixes #112702

@steveharter steveharter added enhancement Product code improvement that does NOT require public API changes/additions area-Extensions-DependencyInjection labels Apr 17, 2025
@steveharter steveharter added this to the 10.0.0 milestone Apr 17, 2025
@steveharter steveharter requested a review from tarekgh April 17, 2025 18:44
@steveharter steveharter self-assigned this Apr 17, 2025
@Copilot Copilot AI review requested due to automatic review settings April 17, 2025 18:44
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • src/libraries/Microsoft.Extensions.DependencyInjection/src/Resources/Strings.resx: Language not supported

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.

@@ -195,4 +195,7 @@
<data name="KeyedServiceAnyKeyUsedToResolveService" xml:space="preserve">
<value>KeyedService.AnyKey cannot be used to resolve a single service.</value>
</data>
<data name="NoKeyedServiceRegistered" xml:space="preserve">
<value>No keyed service for type '{0}' using key type '{1}' has been registered.</value>
Copy link
Member

@stephentoub stephentoub Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to include a ToString()'d key, only its type? Seems like one of the most common types of keys is string, and that string value would be more useful than "System.String". Is it that we're concerned about leaking details in the error message?

Copy link
Contributor Author

@steveharter steveharter Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically don't show disclose (possible) user data, but type names are OK.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modulo @stephentoub comment, LGTM.

@steveharter
Copy link
Contributor Author

/ba-g infra build timeouts

@steveharter steveharter merged commit 7d247dd into dotnet:main Apr 18, 2025
76 of 87 checks passed
@steveharter steveharter deleted the DiImproveMessage branch April 18, 2025 15:52
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-DependencyInjection enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Imprecise exception message when resolving keyed services.
3 participants