Skip to content

Support Keyed Services in Minimal APIs #49633

Closed
@adityamandaleeka

Description

@adityamandaleeka

Keyed services support went into M.E.DI in Preview 7. We should support it in minimal APIs, to make things like the following work:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddKeyedSingleton<ICache, BigCache>("big");
builder.Services.AddKeyedSingleton<ICache, SmallCache>("small");

var app = builder.Build();

app.MapGet("/big", ([FromKeyedServices("big")] ICache cache) =>
{
    return cache.Get("data");
});

app.MapGet("/small", ([FromKeyedServices("small")] ICache cache) =>
{
    return cache.Get("data");
});

app.Run();

interface ICache
{
    object Get(string key);
}
class BigCache : ICache
{
    public object Get(string key)
    {
        throw new NotImplementedException();
    }
}

class SmallCache : ICache
{
    public object Get(string key)
    {
        throw new NotImplementedException();
    }
}

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-rdffeature-rdg

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions