Skip to content

RemoveAllKeyed Requires Equality Operator #95754

Closed

Description

@jdaspinwall

Description

.NET 8 introduced the ability to register keyed services and works reasonably well, however, there seems to be a bug in ServiceCollectionDescriptorExtensions.Keyed.cs within the IServiceCollection RemoveAllKeyed(this IServiceCollection collection, Type serviceType, object? serviceKey) method that breaks objects that don't overload the equals operator.

Reproduction Steps

Given an enumeration, for example:

public enum StorageType
{
    Local,
    Cloud
}

The following test will fail:

var services = new ServiceCollection()
    .AddKeyedTransient<IStorage, LocalStorage>(StorageType.Local)
    .AddKeyedTransient<IStorage, CloudStorage>(StorageType.Cloud);

services.RemoveAllKeyed<IStorage>(StorageType.Cloud);

// test succeeds
Assert.Contains(services, descriptor => descriptor.ServiceType == typeof(IStorage) && (descriptor.ServiceKey?.Equals(StorageType.Local) ?? false));
// test fails - service still exists
Assert.DoesNotContain(services, descriptor => descriptor.ServiceType == typeof(IStorage) && (descriptor.ServiceKey?.Equals(StorageType.Cloud) ?? false));

Expected behavior

The IServiceCollection RemoveAllKeyed(this IServiceCollection collection, Type serviceType, object? serviceKey) should remove any service types matching the specified key regardless of whether the type overloads the equals operator.

Actual behavior

The IServiceCollection RemoveAllKeyed(this IServiceCollection collection, Type serviceType, object? serviceKey) does not remove any service types matching the specified key if the type does not overload the equality operator.

Regression?

No response

Known Workarounds

The problem can be solved by changing the equality operator in this method to the Equals method:

public static IServiceCollection RemoveAllKeyed(this IServiceCollection collection, Type serviceType, Enum? serviceKey)
{
    serviceType.CheckNotNull(nameof(serviceType));

    for (var i = collection.Count - 1; i >= 0; i--)
    {
        var descriptor = collection[i];

        if (descriptor.ServiceType == serviceType && (descriptor.ServiceKey?.Equals(serviceKey) ?? false))
        {
            collection.RemoveAt(i);
        }
    }

    return collection;
}

The workaround for now is to implement another extension method that uses the explicit key type for the key parameter instead of just object.

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Dec 7, 2023
  2. ghost added
    in-pr
    on Dec 8, 2023
  3. tommysor commented on Dec 9, 2023

    @tommysor
    Contributor

    Certainly looks like a bug to me, also affects ServiceKey of type int, decimal, etc.
    Probably affects all keys except type string.
    I created a pr that should make the matching behavior of RemoveAllKeyed the same as for resolving the service with GetRequiredKeyedService.
    Which is basically identical to the original posters "Known Workarounds" solution.

    Edit: Also affects methods Replace, TryAddEnumerable and TryAdd

  4. steveharter commented on Dec 13, 2023

    @steveharter
    Contributor
  5. added this to the 9.0.0 milestone on Dec 13, 2023
  6. ghost removed
    untriaged
    on Dec 13, 2023
  7. ghost removed
    in-pr
    on Jan 11, 2024
  8. locked and limited conversation to collaborators on Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions