---
title: "What is AddKeyedTransient?"  
description: "What is AddKeyedTransient?"  
author: "Anubhav Sharma"  
published: 2026-08-04  
updated: 2026-08-04  
canonical: https://www.mindstick.com/interview/34531/what-is-addkeyedtransient  
category: "asp.net core"  
tags: ["c#", "asp.net core"]  
reading_time: 4 minutes  

---

# What is AddKeyedTransient?

`AddKeyedTransient` is a [dependency injection (DI)](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0) 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), `AddKeyedTransient` allows you to resolve a specific implementation using a unique key such as a string, enum, or other object.

### Syntax

```cs
builder.Services.AddKeyedTransient<IMessageService, EmailService>("email");
builder.Services.AddKeyedTransient<IMessageService, SmsService>("sms");
```

### Resolving a Keyed Service

You can inject a specific implementation using the `[FromKeyedServices]` attribute.

```cs
public class NotificationController : ControllerBase
{
    private readonly IMessageService _messageService;

    public NotificationController(
        [FromKeyedServices("email")] IMessageService messageService)
    {
        _messageService = messageService;
    }
}
```

### Example

#### Interface

```cs
public interface IMessageService
{
    void Send(string message);
}
```

#### Implementations

```cs
public class EmailService : IMessageService
{
    public void Send(string message)
    {
        Console.WriteLine($"Email: {message}");
    }
}

public class SmsService : IMessageService
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS: {message}");
    }
}
```

#### Registration

```cs
builder.Services.AddKeyedTransient<IMessageService, EmailService>("email");
builder.Services.AddKeyedTransient<IMessageService, SmsService>("sms");
```

#### Injection

```cs
public class NotificationService
{
    private readonly IMessageService _emailService;
    private readonly IMessageService _smsService;

    public NotificationService(
        [FromKeyedServices("email")] IMessageService emailService,
        [FromKeyedServices("sms")] IMessageService smsService)
    {
        _emailService = emailService;
        _smsService = smsService;
    }

    public void Notify()
    {
        _emailService.Send("Welcome!");
        _smsService.Send("OTP: 123456");
    }
}
```

### Lifetime

`AddKeyedTransient` creates a **new instance every time the keyed service is requested**, just like `AddTransient`.

### When to Use `AddKeyedTransient`

- Register multiple implementations of the same interface.
- Select an implementation based on a known key.
- Replace manual service factories or large `switch` statements.
- Support scenarios like different payment gateways, notification providers, or storage services.

### Comparison

| Method | Lifetime | Supports Keys | Use Case |
| --- | --- | --- | --- |
| `AddTransient` | New instance every request | No | Single implementation |
| `AddKeyedTransient` | New instance every request | Yes | Multiple implementations with keys |
| `AddScoped` | One instance per request | No | Request-scoped services |
| `AddKeyedScoped` | One instance per request | Yes | Keyed request-scoped services |
| `AddSingleton` | One instance for application lifetime | No | Shared services |
| `AddKeyedSingleton` | One shared instance per key | Yes | Shared keyed services |

## Answers

### Answer by Anubhav Sharma

`AddKeyedTransient` is a [dependency injection (DI)](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0) 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), `AddKeyedTransient` allows you to resolve a specific implementation using a unique key such as a string, enum, or other object.

### Syntax

```cs
builder.Services.AddKeyedTransient<IMessageService, EmailService>("email");
builder.Services.AddKeyedTransient<IMessageService, SmsService>("sms");
```

### Resolving a Keyed Service

You can inject a specific implementation using the `[FromKeyedServices]` attribute.

```cs
public class NotificationController : ControllerBase
{
    private readonly IMessageService _messageService;

    public NotificationController(
        [FromKeyedServices("email")] IMessageService messageService)
    {
        _messageService = messageService;
    }
}
```

### Example

#### Interface

```cs
public interface IMessageService
{
    void Send(string message);
}
```

#### Implementations

```cs
public class EmailService : IMessageService
{
    public void Send(string message)
    {
        Console.WriteLine($"Email: {message}");
    }
}

public class SmsService : IMessageService
{
    public void Send(string message)
    {
        Console.WriteLine($"SMS: {message}");
    }
}
```

#### Registration

```cs
builder.Services.AddKeyedTransient<IMessageService, EmailService>("email");
builder.Services.AddKeyedTransient<IMessageService, SmsService>("sms");
```

#### Injection

```cs
public class NotificationService
{
    private readonly IMessageService _emailService;
    private readonly IMessageService _smsService;

    public NotificationService(
        [FromKeyedServices("email")] IMessageService emailService,
        [FromKeyedServices("sms")] IMessageService smsService)
    {
        _emailService = emailService;
        _smsService = smsService;
    }

    public void Notify()
    {
        _emailService.Send("Welcome!");
        _smsService.Send("OTP: 123456");
    }
}
```

### Lifetime

`AddKeyedTransient` creates a **new instance every time the keyed service is requested**, just like `AddTransient`.

### When to Use `AddKeyedTransient`

- Register multiple implementations of the same interface.
- Select an implementation based on a known key.
- Replace manual service factories or large `switch` statements.
- Support scenarios like different payment gateways, notification providers, or storage services.

### Comparison

| Method | Lifetime | Supports Keys | Use Case |
| --- | --- | --- | --- |
| `AddTransient` | New instance every request | No | Single implementation |
| `AddKeyedTransient` | New instance every request | Yes | Multiple implementations with keys |
| `AddScoped` | One instance per request | No | Request-scoped services |
| `AddKeyedScoped` | One instance per request | Yes | Keyed request-scoped services |
| `AddSingleton` | One instance for application lifetime | No | Shared services |
| `AddKeyedSingleton` | One shared instance per key | Yes | Shared keyed services |


---

Original Source: https://www.mindstick.com/interview/34531/what-is-addkeyedtransient

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
