What is AddKeyedTransient?
Ask by Anubhav Sharma
Updated 14 Aug 2026
AddKeyedTransientlets you register multiple implementations of the same interface using different keys, with a Transient lifetime.Then you can request one by its key:
In simple words:
AddKeyedTransientis a dependency injection (DI) registration method in ASP.NET Core that lets you register multiple implementations of the same service type, each identified by a key. It was introduced in .NET 8 as part of the new keyed services feature.Unlike
AddTransient, which registers a single implementation (or multiple without an easy way to distinguish them),AddKeyedTransientallows you to resolve a specific implementation using a unique key such as a string, enum, or other object.Syntax
Resolving a Keyed Service
You can inject a specific implementation using the
[FromKeyedServices]attribute.Example
Interface
Implementations
Registration
Injection
Lifetime
AddKeyedTransientcreates a new instance every time the keyed service is requested, just likeAddTransient.When to Use
AddKeyedTransientswitchstatements.Comparison
AddTransientAddKeyedTransientAddScopedAddKeyedScopedAddSingletonAddKeyedSingleton