Skip to content

Commit f3edf6e

Browse files
authored
Add breaking change documentation for FromKeyedServicesAttribute.Key nullability (#48778)
1 parent 3b4ddd8 commit f3edf6e

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

docs/core/compatibility/8.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ If you're migrating an app to .NET 8, the breaking changes listed here might aff
9191
| [ConfigurationManager package no longer references System.Security.Permissions](extensions/8.0/configurationmanager-package.md) | Source incompatible |
9292
| [DirectoryServices package no longer references System.Security.Permissions](extensions/8.0/directoryservices-package.md) | Source incompatible |
9393
| [Empty keys added to dictionary by configuration binder](extensions/8.0/dictionary-configuration-binding.md) | Behavioral change |
94+
| [FromKeyedServicesAttribute.Key can be null](extensions/8.0/fromkeyedservicesattribute-key-nullable.md) | Source incompatible |
9495
| [HostApplicationBuilderSettings.Args respected by HostApplicationBuilder ctor](extensions/8.0/hostapplicationbuilder-ctor.md) | Behavioral change |
9596
| [ManagementDateTimeConverter.ToDateTime returns a local time](extensions/8.0/dmtf-todatetime.md) | Behavioral change |
9697
| [System.Formats.Cbor DateTimeOffset formatting change](extensions/8.0/cbor-datetime.md) | Behavioral change |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: "Breaking change: FromKeyedServicesAttribute.Key can be null"
3+
description: "Learn about the breaking change in .NET 8 where FromKeyedServicesAttribute.Key is now nullable to support unkeyed services and inheritance."
4+
ms.date: 09/29/2025
5+
ai-usage: ai-assisted
6+
---
7+
8+
# FromKeyedServicesAttribute.Key can be null
9+
10+
<xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> has been changed from a non-nullable `object` to a nullable `object?` to support null values for unkeyed services and inheritance scenarios.
11+
12+
## Version introduced
13+
14+
.NET 8
15+
16+
## Previous behavior
17+
18+
Previously, <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> was declared as a non-nullable `object`:
19+
20+
```csharp
21+
public object Key { get; }
22+
```
23+
24+
## New behavior
25+
26+
Starting in .NET 8, <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> is now declared as a nullable `object?`:
27+
28+
```csharp
29+
public object? Key { get; }
30+
```
31+
32+
A `null` value indicates there is no key and only the parameter type is used to resolve the service. This is useful for dependency injection implementations that require an explicit way to declare that the parameter should be resolved for unkeyed services. A `null` value is also used with inheritance scenarios to indicate that the key should be inherited from the parent scope.
33+
34+
## Type of breaking change
35+
36+
This change can affect [source compatibility](../../categories.md#source-compatibility).
37+
38+
## Reason for change
39+
40+
Support was added for keyed services to annotate parameters as unkeyed. This change allows developers to explicitly indicate when a parameter should be resolved without a key, which is particularly useful in scenarios where both keyed and unkeyed services are registered for the same type.
41+
42+
## Recommended action
43+
44+
Adjust any code that uses <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> to handle `null` values.
45+
46+
## Affected APIs
47+
48+
- <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ items:
436436
href: extensions/8.0/directoryservices-package.md
437437
- name: Empty keys added to dictionary by configuration binder
438438
href: extensions/8.0/dictionary-configuration-binding.md
439+
- name: FromKeyedServicesAttribute.Key can be null
440+
href: extensions/8.0/fromkeyedservicesattribute-key-nullable.md
439441
- name: HostApplicationBuilderSettings.Args respected by constructor
440442
href: extensions/8.0/hostapplicationbuilder-ctor.md
441443
- name: ManagementDateTimeConverter.ToDateTime returns a local time

0 commit comments

Comments
 (0)