Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 1.2 KB

File metadata and controls

39 lines (27 loc) · 1.2 KB

Automatic Custom Keyed Service Registration

This project demonstrates how to implement automatic custom keyed service registration using the IServiceCollection extension method.

The example includes a simple implementation of a service that can be registered with a custom key and retrieved later.

Runtime keyed resolution

var smtp = provider.GetRequiredKeyedService<IEmailSender>("smtp");
var ses = provider.GetRequiredKeyedService<IEmailSender>("ses");

With injection:

using Microsoft.Extensions.DependencyInjection;

public class NotificationService
{
    public NotificationService([FromKeyedServices("smtp")] IEmailSender emailSender)
    { }
}