Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
RemoveAllKeyed use Equals for matching
This brings RemoveAllKeyed in line with service resolution (ServiceProvider.GetRequiredKeyedService)
  • Loading branch information
tommysor authored and github-actions committed Jan 11, 2024
commit 2a7efbd75d8849a1355c1e0e22cba3791f851ac9
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public static IServiceCollection RemoveAllKeyed(this IServiceCollection collecti
for (int i = collection.Count - 1; i >= 0; i--)
{
ServiceDescriptor? descriptor = collection[i];
if (descriptor.ServiceType == serviceType && descriptor.ServiceKey == serviceKey)
if (descriptor.ServiceType == serviceType && object.Equals(descriptor.ServiceKey, serviceKey))
{
collection.RemoveAt(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,62 @@ public void RemoveAll_RemovesAllServicesWithMatchingServiceType()
Assert.Equal(new[] { descriptor }, collection);
}

private enum ServiceKeyEnum { First, Second }

[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsEnum()
{
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.First, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.Second, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.Second, typeof(FakeService), ServiceLifetime.Transient),
};

// Act
collection.RemoveAllKeyed<IFakeService>(ServiceKeyEnum.Second);

// Assert
Assert.Contains(collection, x => ServiceKeyEnum.First.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => ServiceKeyEnum.Second.Equals(x.ServiceKey));
}

[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsInt()
{
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2, typeof(FakeService), ServiceLifetime.Transient),
};

// Act
collection.RemoveAllKeyed<IFakeService>(2);

// Assert
Assert.Contains(collection, x => 1.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => 2.Equals(x.ServiceKey));
}

[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsDouble()
{
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), 1.0, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2.0, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2.0, typeof(FakeService), ServiceLifetime.Transient),
};

// Act
collection.RemoveAllKeyed<IFakeService>(2.0);

// Assert
Assert.Contains(collection, x => 1.0.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => 2.0.Equals(x.ServiceKey));
}

public static TheoryData NullServiceKeyData
{
get
Expand Down