dotnet / dotnet/aspnetcore

.NET 8 FromKeyedServices does not apply registered IConfigureOptions<T> with a key to IOptions<T>

Open
#54,476 0 comments 0 reactions 0 assignees View on GitHub
area-minimal area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

In .NET 8, when using the new **FromKeyedServices** attribute to inject **IOptions** with specific configurations based on a key, it appears that **IConfigureOptions** services registered with the same key are not being correctly applied. This behavior is unexpected as the purpose of **FromKeyedServices** is to allow keyed service injection, and one would expect that keyed configurations would be applied accordingly.

Actual Behavior:
The injected **IOptions** instance does not reflect the configurations applied by the keyed **IConfigureOptions** registration. Instead, it seems to fall back to default or non-keyed configurations, suggesting that the **FromKeyedServices** attribute does not correctly resolve keyed **IConfigureOptions** services.

### Expected Behavior

When using **[FromKeyedServices("test")]** to inject **IOptions**, the instance of **IOptions** should reflect the configurations applied by any **IConfigureOptions** registered with the key "test". This would allow for dynamic configuration scenarios where different settings can be applied based on the key specified at injection time.

### Steps To Reproduce

1. Register a configuration class **MyOptions** and its **IConfigureOptions** implementation with a specific key (e.g., "test") in the DI container.
2. Use the **[FromKeyedServices("test")]** attribute to inject **IOptions** into a service or controller.
3. Observe that the **IOptions** instance does not reflect the configurations applied by the keyed **IConfigureOptions** registration.

### Exceptions (if any)

_No response_

### .NET Version

8

### Anything else?

.NET version: .NET 8
Operating system: Windows 11
IDE: VS 2022

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.