Skip to content

Imprecise exception message when resolving keyed services. #112702

@Yawnder

Description

@Yawnder

In ServiceProvider, when attempting to resolve a keyed service for which you haven't registered that key, you get an error message (SR.NoServiceRegistered) saying "No service for type '{type}' has been registered.":

object? service = GetKeyedService(serviceType, serviceKey, serviceProviderEngineScope);
if (service == null)
{
throw new InvalidOperationException(SR.Format(SR.NoServiceRegistered, serviceType));
}

When serviceKey != null, the error message should mention something along the lines of "for the serviceKey".

I understand that the key can't necessarily be displayed, but just mentioning that would help solve the issue causing the exception.

Having the message conditional on serviceKey being null or not adds a bit a processing, but considering that it's already on a failing path, while throwing an exception, I think it's acceptable..

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions