How to use keyed services in ASP.NET Core

var builder = WebApplication.CreateBuilder(args);
// Register multiple keyed services for the ICustomLogger interface
builder.Services.AddKeyedScoped("file");
builder.Services.AddKeyedScoped("database");
builder.Services.AddKeyedScoped("event");
var app = builder.Build();

Note how the FileLogger, DatabaseLogger, and EventLogger services are registered using the keys "file", "database", and "event", respectively.

Inject the keyed logger services

We can use the [FromKeyedServices] attribute to inject a specific implementation of our logger service in our minimal API endpoints as shown in the code snippet given below.

app.MapGet("/customlogger/file", ([FromKeyedServices("file")] ICustomLogger fileLogger) =>
{
    fileLogger.Log("This text is written to the file system.");
    return Results.Ok("File logger executed successfully.");
});
app.MapGet("/customlogger/db", ([FromKeyedServices("database")] ICustomLogger databaseLogger) =>
{
    databaseLogger.Log("This text is stored in the database.");
    return Results.Ok("Database logger executed successfully.");
});
app.MapGet("/customlogger/event", ([FromKeyedServices("event")] ICustomLogger logger) =>
{
    logger.Log("This text is recorded in the event system.");
    return Results.Ok("Event logger executed successfully.");
});

Thus, by using DI and keyed services, we can implement each of our logger services once, then simply ask for the right type of the logger when we need one without having to use a factory to instantate the logger. And whenever we want to swap the implementations—from FileLogger to DatabaseLogger, for example—all we need to do is change the configuration we specied while registering the services with the container. The DI system will plug in the right logger automatically at run time.

Donner Music, make your music with gear
Multi-Function Air Blower: Blowing, suction, extraction, and even inflation

Leave a reply

Please enter your comment!
Please enter your name here