From 7e2c171a222ae16e606edb0a5bb94c2527c60fc5 Mon Sep 17 00:00:00 2001 From: Martin Taillefer Date: Fri, 11 Aug 2023 09:21:25 -0700 Subject: [PATCH] Cleanup AsyncState API (#4276) Co-authored-by: Martin Taillefer --- .../AsyncStateHttpContextExtensions.cs | 6 +- .../AsyncStateExtensions.cs | 32 -------- .../Microsoft.Extensions.AsyncState.json | 4 - ...ContextServiceCollectionExtensionsTests.cs | 73 ------------------- 4 files changed, 2 insertions(+), 113 deletions(-) diff --git a/src/Libraries/Microsoft.AspNetCore.AsyncState/AsyncStateHttpContextExtensions.cs b/src/Libraries/Microsoft.AspNetCore.AsyncState/AsyncStateHttpContextExtensions.cs index c4301e85b23..8f948c6a442 100644 --- a/src/Libraries/Microsoft.AspNetCore.AsyncState/AsyncStateHttpContextExtensions.cs +++ b/src/Libraries/Microsoft.AspNetCore.AsyncState/AsyncStateHttpContextExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.Extensions.AsyncState; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Shared.Diagnostics; namespace Microsoft.AspNetCore.AsyncState; @@ -25,11 +24,10 @@ public static IServiceCollection AddAsyncStateHttpContext(this IServiceCollectio { _ = Throw.IfNull(services); - services + _ = services .AddHttpContextAccessor() .AddAsyncStateCore() - .TryRemoveAsyncStateCore() - .TryAddSingleton(typeof(IAsyncContext<>), typeof(AsyncContextHttpContext<>)); + .AddSingleton(typeof(IAsyncContext<>), typeof(AsyncContextHttpContext<>)); return services; } diff --git a/src/Libraries/Microsoft.Extensions.AsyncState/AsyncStateExtensions.cs b/src/Libraries/Microsoft.Extensions.AsyncState/AsyncStateExtensions.cs index 4907cd931bf..76fe144e5c5 100644 --- a/src/Libraries/Microsoft.Extensions.AsyncState/AsyncStateExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.AsyncState/AsyncStateExtensions.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.ComponentModel; -using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Shared.Diagnostics; @@ -31,34 +29,4 @@ public static IServiceCollection AddAsyncStateCore(this IServiceCollection servi return services; } - - /// - /// Tries to remove the default implementation for , , and services. - /// - /// The dependency injection container to remove the implementations from. - /// The value of . - /// is . - [EditorBrowsable(EditorBrowsableState.Never)] - public static IServiceCollection TryRemoveAsyncStateCore(this IServiceCollection services) - { - _ = Throw.IfNull(services); - - services.TryRemoveSingleton(typeof(IAsyncContext<>), typeof(AsyncContext<>)); - - return services; - } - - internal static void TryRemoveSingleton( - this IServiceCollection services, - Type serviceType, - Type implementationType) - { - var descriptor = services.FirstOrDefault( - x => (x.ServiceType == serviceType) && (x.ImplementationType == implementationType)); - - if (descriptor != null) - { - _ = services.Remove(descriptor); - } - } } diff --git a/src/Libraries/Microsoft.Extensions.AsyncState/Microsoft.Extensions.AsyncState.json b/src/Libraries/Microsoft.Extensions.AsyncState/Microsoft.Extensions.AsyncState.json index 5a85c4dde23..366e9624900 100644 --- a/src/Libraries/Microsoft.Extensions.AsyncState/Microsoft.Extensions.AsyncState.json +++ b/src/Libraries/Microsoft.Extensions.AsyncState/Microsoft.Extensions.AsyncState.json @@ -8,10 +8,6 @@ { "Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.AsyncState.AsyncStateExtensions.AddAsyncStateCore(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);", "Stage": "Stable" - }, - { - "Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.AsyncState.AsyncStateExtensions.TryRemoveAsyncStateCore(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);", - "Stage": "Stable" } ] }, diff --git a/test/Libraries/Microsoft.Extensions.AsyncState.Tests/AsyncContextServiceCollectionExtensionsTests.cs b/test/Libraries/Microsoft.Extensions.AsyncState.Tests/AsyncContextServiceCollectionExtensionsTests.cs index 651dc2b8130..c030ceed662 100644 --- a/test/Libraries/Microsoft.Extensions.AsyncState.Tests/AsyncContextServiceCollectionExtensionsTests.cs +++ b/test/Libraries/Microsoft.Extensions.AsyncState.Tests/AsyncContextServiceCollectionExtensionsTests.cs @@ -4,7 +4,6 @@ using System; using System.Linq; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Xunit; namespace Microsoft.Extensions.AsyncState.Test; @@ -36,76 +35,4 @@ public void AddAsyncStateCore_AddsWithCorrectLifetime() serviceDescriptor = services.First(x => x.ServiceType == typeof(IAsyncLocalContext<>)); Assert.Equal(ServiceLifetime.Singleton, serviceDescriptor.Lifetime); } - - [Fact] - public void TryRemoveAsyncStateCore_Throws_WhenNullService() - { - Assert.Throws(() => AsyncStateExtensions.TryRemoveAsyncStateCore(null!)); - } - - [Fact] - public void TryRemoveAsyncStateCore_RemovesAsyncContext() - { - var services = new ServiceCollection(); - - services.AddAsyncStateCore(); - - Assert.NotNull(services.FirstOrDefault(x => - (x.ServiceType == typeof(IAsyncContext<>)) && (x.ImplementationType == typeof(AsyncContext<>)))); - - services.TryRemoveAsyncStateCore(); - - Assert.Null(services.FirstOrDefault(x => - (x.ServiceType == typeof(IAsyncContext<>)) && (x.ImplementationType == typeof(AsyncContext<>)))); - } - - [Fact] - public void TryRemoveSingleton_DoesNothingToEmptyServices() - { - var services = new ServiceCollection(); - - services.TryRemoveSingleton(typeof(IThing), typeof(Thing)); - - Assert.Empty(services); - } - - [Fact] - public void TryRemoveSingleton_RemovesWhenPresent() - { - var services = new ServiceCollection(); - - services.TryAddSingleton(); - - Assert.Single(services); - var descriptor = services[0]; - Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime); - Assert.Equal(typeof(IThing), descriptor.ServiceType); - Assert.Equal(typeof(Thing), descriptor.ImplementationType); - - services.TryRemoveSingleton(typeof(IThing), typeof(Thing)); - - Assert.Empty(services); - } - - [Fact] - public void TryRemoveSingleton_DoesNotRemoveOtherThanSpecified() - { - var services = new ServiceCollection(); - - services.TryAddSingleton(); - - Assert.Single(services); - var descriptor = services[0]; - Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime); - Assert.Equal(typeof(IThing), descriptor.ServiceType); - Assert.Equal(typeof(Thing), descriptor.ImplementationType); - - services.TryRemoveSingleton(typeof(IThing), typeof(AnotherThing)); - - Assert.Single(services); - var descriptor2 = services[0]; - Assert.Equal(ServiceLifetime.Singleton, descriptor2.Lifetime); - Assert.Equal(typeof(IThing), descriptor2.ServiceType); - Assert.Equal(typeof(Thing), descriptor2.ImplementationType); - } }