Skip to content

Commit

Permalink
Cleanup AsyncState API
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Taillefer committed Aug 11, 2023
1 parent 59ee50b commit 00a26b0
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,34 +29,4 @@ public static IServiceCollection AddAsyncStateCore(this IServiceCollection servi

return services;
}

/// <summary>
/// Tries to remove the default implementation for <see cref="IAsyncState"/>, <see cref="IAsyncContext{T}"/>, and <see cref="IAsyncLocalContext{T}"/> services.
/// </summary>
/// <param name="services">The dependency injection container to remove the implementations from.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null" />.</exception>
[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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ArgumentNullException>(() => 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<IThing, Thing>();

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<IThing, Thing>();

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);
}
}

0 comments on commit 00a26b0

Please sign in to comment.