Keyed Services (Named registrations) in .NET Service Provider
Read OriginalThis 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.
Comments
No comments yet
Be the first to share your thoughts!
Browser Extension
Get instant access to AllDevBlogs from your browser