Skip to content

Add a way to get IReadOnlyDictionary<,> of keyed services #95476

Open
@MikeAlhayek

Description

@MikeAlhayek

.NET 8 introduced a way to register keyed services which is great addition. However, what if one wants to get a list of registered services along with their key. For example, during run time, I want to present the user a menu of some sort with all available services. The user will select the default service to use for the app. Without a way to expose the registered key, there is no real easy way to identify the keyed service and its key.

I think the missing piece here is the ability to be able to inject IReadOnlyDictionary<object?, object> which it'll be resolved by the IoC container. Alternatively, we can have a service with something like this

public interface IKeyedServiceProvider
{
    IReadOnlyDictionary<object?, T> GetServices<T>();
}

Along with extensions similar to the following

IReadOnlyDictionary<TKey, T> GetServices<TKey, T>()

Or add an extension on IServiceProvider that allows the above 2 methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-DependencyInjectionneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions