diff --git a/docs/core/compatibility/8.0.md b/docs/core/compatibility/8.0.md index 6c56c864ba3aa..15b7c857b2109 100644 --- a/docs/core/compatibility/8.0.md +++ b/docs/core/compatibility/8.0.md @@ -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 | diff --git a/docs/core/compatibility/extensions/8.0/fromkeyedservicesattribute-key-nullable.md b/docs/core/compatibility/extensions/8.0/fromkeyedservicesattribute-key-nullable.md new file mode 100644 index 0000000000000..b408b9378898f --- /dev/null +++ b/docs/core/compatibility/extensions/8.0/fromkeyedservicesattribute-key-nullable.md @@ -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 + + 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, was declared as a non-nullable `object`: + +```csharp +public object Key { get; } +``` + +## New behavior + +Starting in .NET 8, 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 to handle `null` values. + +## Affected APIs + +- diff --git a/docs/core/compatibility/toc.yml b/docs/core/compatibility/toc.yml index 33939a4065721..2533b4153a347 100644 --- a/docs/core/compatibility/toc.yml +++ b/docs/core/compatibility/toc.yml @@ -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