Skip to content

Improve error message when GetRequiredKeyedService() fails#114796

Merged
steveharter merged 1 commit into
dotnet:mainfrom
steveharter:DiImproveMessage
Apr 18, 2025
Merged

Improve error message when GetRequiredKeyedService() fails#114796
steveharter merged 1 commit into
dotnet:mainfrom
steveharter:DiImproveMessage

Conversation

@steveharter

Copy link
Copy Markdown
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 AI review requested due to automatic review settings April 17, 2025 18:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

<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>

@stephentoub stephentoub Apr 17, 2025

Copy link
Copy Markdown
Member

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?

@steveharter steveharter Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

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.

@tarekgh tarekgh left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor Author

/ba-g infra build timeouts

@steveharter steveharter merged commit 7d247dd into dotnet:main Apr 18, 2025
@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.

4 participants