Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/core/compatibility/8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ If you're migrating an app to .NET 8, the breaking changes listed here might aff
| [ConfigurationManager package no longer references System.Security.Permissions](extensions/8.0/configurationmanager-package.md) | Source incompatible |
| [DirectoryServices package no longer references System.Security.Permissions](extensions/8.0/directoryservices-package.md) | Source incompatible |
| [Empty keys added to dictionary by configuration binder](extensions/8.0/dictionary-configuration-binding.md) | Behavioral change |
| [FromKeyedServicesAttribute.Key can be null](extensions/8.0/fromkeyedservicesattribute-key-nullable.md) | Source incompatible |
| [HostApplicationBuilderSettings.Args respected by HostApplicationBuilder ctor](extensions/8.0/hostapplicationbuilder-ctor.md) | Behavioral change |
| [ManagementDateTimeConverter.ToDateTime returns a local time](extensions/8.0/dmtf-todatetime.md) | Behavioral change |
| [System.Formats.Cbor DateTimeOffset formatting change](extensions/8.0/cbor-datetime.md) | Behavioral change |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "Breaking change: FromKeyedServicesAttribute.Key can be null"
description: "Learn about the breaking change in .NET 8 where FromKeyedServicesAttribute.Key is now nullable to support unkeyed services and inheritance."
ms.date: 09/29/2025
ai-usage: ai-assisted
---

# FromKeyedServicesAttribute.Key can be null

<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.

## Version introduced

.NET 8

## Previous behavior

Previously, <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> was declared as a non-nullable `object`:

```csharp
public object Key { get; }
```

## New behavior

Starting in .NET 8, <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> is now declared as a nullable `object?`:

```csharp
public object? Key { get; }
```

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.

## Type of breaking change

This change can affect [source compatibility](../../categories.md#source-compatibility).

## Reason for change

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.

## Recommended action

Adjust any code that uses <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=nameWithType> to handle `null` values.

## Affected APIs

- <xref:Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute.Key?displayProperty=fullName>
2 changes: 2 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ items:
href: extensions/8.0/directoryservices-package.md
- name: Empty keys added to dictionary by configuration binder
href: extensions/8.0/dictionary-configuration-binding.md
- name: FromKeyedServicesAttribute.Key can be null
href: extensions/8.0/fromkeyedservicesattribute-key-nullable.md
- name: HostApplicationBuilderSettings.Args respected by constructor
href: extensions/8.0/hostapplicationbuilder-ctor.md
- name: ManagementDateTimeConverter.ToDateTime returns a local time
Expand Down