Skip to content

[API Proposal]: Support resolving all service keys associated with keyed DI instances #100105

Open
@captainsafia

Description

@captainsafia

Background and motivation

I'm using keyed DI to manage multiple versions of a document in a DI-based store. As part of this work, I need to implement a API that provides information to the end-user about allt the version names (aka service keys) that are associated with a give document.

I can use the GetKeyedServices<MyType>(KeyedService.AnyKey) API to retrieve all instances associated with my service type but there is no way to resolve the service key associated with each type as a result of it.

cc: @benjaminpetit

API Proposal

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceProviderKeyedServiceExtensions
{
    public static IEnumerable<object> GetKeyedServiceKeys<T>(this IServiceProvider provider)
}

API Usage

public IEnumerable<string> GetDocumentName()
{
	return services.GetKeyedServiceKeys<DocumentType>();
}

Alternative Designs

The existing GetKeyedServices extension method can be updated to return a tuple capturing the service key.

- public static IEnumerable<T> GetKeyedServices<T>(this IServiceProvider provider, object? serviceKey)
+ public static IEnumerable<(object, T)> GetKeyedServices<T>(this IServiceProvider provider, object? serviceKey)

Risks

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions