-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds extension method to create service scope that implements IAsyncD…
…isposable. (#51840) * Adds extension method to create service scope that implements IAsyncDisposable. - Introduces a new type AsyncServiceScope that implements IServiceScope and IAsyncDisposable. The type just wraps an existing IServiceScope instance, which it tries to cast it to an IAsyncDisposable when DisposeAsync is called. - Adds netstandard2.1 target to avoid bringing in System.Threading.Tasks.Extensions and Microsoft.Bcl.AsyncInterfaces if not needed. - Fixes #43970 * Make AsyncServiceScope readonly Co-authored-by: David Fowler <davidfowl@gmail.com> * Use null-coalescing for null checking and argument exception. Co-authored-by: David Fowler <davidfowl@gmail.com> * Make AsyncServiceScope readonly in reference source. * Adds tests for AsyncServiceScope and CreateAsyncScope extension method. * Merge generated ref source. * Document why 'default' is used instead of 'ValueTask.CompletedTask' * Remove unnecessary casts to IDisposable and IAsyncDisposable in tests. Co-authored-by: David Fowler <davidfowl@gmail.com>
- Loading branch information
1 parent
9f55689
commit 41f3d48
Showing
8 changed files
with
278 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...cyInjection.Abstractions/ref/Microsoft.Extensions.DependencyInjection.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks> | ||
<TargetFrameworks>netstandard2.1;netstandard2.0;net461</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Microsoft.Extensions.DependencyInjection.Abstractions.cs" /> | ||
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" /> | ||
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or | ||
$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETFramework'"> | ||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" /> | ||
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.AsyncInterfaces\src\Microsoft.Bcl.AsyncInterfaces.csproj" /> | ||
</ItemGroup> | ||
</Project> |
48 changes: 48 additions & 0 deletions
48
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/AsyncServiceScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// A <see cref="IServiceScope" /> implementation that implements <see cref="IAsyncDisposable" />. | ||
/// </summary> | ||
public readonly struct AsyncServiceScope : IServiceScope, IAsyncDisposable | ||
{ | ||
private readonly IServiceScope _serviceScope; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncServiceScope"/> struct. | ||
/// Wraps an instance of <see cref="IServiceScope" />. | ||
/// <param name="serviceScope">The <see cref="IServiceScope"/> instance to wrap.</param> | ||
/// </summary> | ||
public AsyncServiceScope(IServiceScope serviceScope) | ||
{ | ||
_serviceScope = serviceScope ?? throw new ArgumentNullException(nameof(serviceScope)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IServiceProvider ServiceProvider => _serviceScope.ServiceProvider; | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
_serviceScope.Dispose(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public ValueTask DisposeAsync() | ||
{ | ||
if (_serviceScope is IAsyncDisposable ad) | ||
{ | ||
return ad.DisposeAsync(); | ||
} | ||
_serviceScope.Dispose(); | ||
|
||
// ValueTask.CompletedTask is only available in net5.0 and later. | ||
return default; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...braries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/AsyncServiceScopeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection.Tests | ||
{ | ||
public class AsyncServiceScopeTests | ||
{ | ||
[Fact] | ||
public void ThrowsIfServiceScopeIsNull() | ||
{ | ||
var exception = Assert.Throws<ArgumentNullException>(() => new AsyncServiceScope(null)); | ||
Assert.Equal("serviceScope", exception.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsServiceProviderFromWrappedScope() | ||
{ | ||
var wrappedScope = new FakeSyncServiceScope(); | ||
var asyncScope = new AsyncServiceScope(wrappedScope); | ||
|
||
Assert.Same(wrappedScope.ServiceProvider, asyncScope.ServiceProvider); | ||
} | ||
|
||
[Fact] | ||
public void CallsDisposeOnWrappedSyncScopeOnDispose() | ||
{ | ||
var wrappedScope = new FakeSyncServiceScope(); | ||
var asyncScope = new AsyncServiceScope(wrappedScope); | ||
|
||
asyncScope.Dispose(); | ||
|
||
Assert.True(wrappedScope.DisposeCalled); | ||
} | ||
|
||
[Fact] | ||
public async ValueTask CallsDisposeOnWrappedSyncScopeOnDisposeAsync() | ||
{ | ||
var wrappedScope = new FakeSyncServiceScope(); | ||
var asyncScope = new AsyncServiceScope(wrappedScope); | ||
|
||
await asyncScope.DisposeAsync(); | ||
|
||
Assert.True(wrappedScope.DisposeCalled); | ||
} | ||
|
||
[Fact] | ||
public void CallsDisposeOnWrappedAsyncScopeOnDispose() | ||
{ | ||
var wrappedScope = new FakeAsyncServiceScope(); | ||
var asyncScope = new AsyncServiceScope(wrappedScope); | ||
|
||
asyncScope.Dispose(); | ||
|
||
Assert.True(wrappedScope.DisposeCalled); | ||
Assert.False(wrappedScope.DisposeAsyncCalled); | ||
} | ||
|
||
[Fact] | ||
public async ValueTask CallsDisposeAsyncOnWrappedSyncScopeOnDisposeAsync() | ||
{ | ||
var wrappedScope = new FakeAsyncServiceScope(); | ||
var asyncScope = new AsyncServiceScope(wrappedScope); | ||
|
||
await asyncScope.DisposeAsync(); | ||
|
||
Assert.False(wrappedScope.DisposeCalled); | ||
Assert.True(wrappedScope.DisposeAsyncCalled); | ||
} | ||
|
||
public class FakeServiceProvider : IServiceProvider | ||
{ | ||
public object? GetService(Type serviceType) => throw new NotImplementedException(); | ||
} | ||
|
||
public class FakeSyncServiceScope : IServiceScope | ||
{ | ||
public FakeSyncServiceScope() | ||
{ | ||
ServiceProvider = new FakeServiceProvider(); | ||
} | ||
|
||
public IServiceProvider ServiceProvider { get; } | ||
|
||
public bool DisposeCalled { get; private set; } | ||
|
||
public void Dispose() | ||
{ | ||
DisposeCalled = true; | ||
} | ||
} | ||
|
||
public class FakeAsyncServiceScope : FakeSyncServiceScope, IAsyncDisposable | ||
{ | ||
public FakeAsyncServiceScope() : base() | ||
{ | ||
} | ||
|
||
public bool DisposeAsyncCalled { get; private set; } | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
DisposeAsyncCalled = true; | ||
|
||
return default; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters