Edit

Share via


FromKeyedServicesAttribute.Key can be null

FromKeyedServicesAttribute.Key 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, FromKeyedServicesAttribute.Key was declared as a non-nullable object:

public object Key { get; }

New behavior

Starting in .NET 8, FromKeyedServicesAttribute.Key is now declared as a nullable object?:

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.

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.

Adjust any code that uses FromKeyedServicesAttribute.Key to handle null values.

Affected APIs