Maarten Balliauw 8/11/2026

Keyed Services (Named registrations) in .NET Service Provider

Read Original

This article explains the problem of multiple implementations for the same interface in .NET's dependency injection, where the last registration wins by default. It introduces keyed services introduced in .NET 8, which allow attaching a key (string, enum, etc.) to a registration and consuming it with [FromKeyedServices] or by key. It covers registration methods (AddKeyedSingleton, AddKeyedScoped, AddKeyedTransient), factory overloads, and consumption patterns. This solves the ambiguity and improves code clarity and maintainability.

Keyed Services (Named registrations) in .NET Service Provider

Comments

No comments yet

Be the first to share your thoughts!