-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from seesharper/implement-iasyncdisposable
Implement IAsyncDisposable in LightInjectServiceScope
- Loading branch information
Showing
4 changed files
with
74 additions
and
6 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
59 changes: 59 additions & 0 deletions
59
src/LightInject.Microsoft.DependencyInjection.Tests/AsyncDisposableTests.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,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace LightInject.Microsoft.DependencyInjection.Tests | ||
{ | ||
public class AsyncDisposableTests | ||
{ | ||
[Fact] | ||
public async Task ShouldDisposeAsyncDisposable() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
List<object> disposedObjects = new(); | ||
serviceCollection.AddScoped<AsyncDisposable>(sp => new AsyncDisposable(disposedObject => disposedObjects.Add(disposedObject))); | ||
|
||
var serviceProvider = serviceCollection.CreateLightInjectServiceProvider(); | ||
|
||
AsyncDisposable asyncDisposable = null; | ||
await using (var scope = serviceProvider.CreateAsyncScope()) | ||
{ | ||
asyncDisposable = scope.ServiceProvider.GetService<AsyncDisposable>(); | ||
} | ||
|
||
Assert.Contains(asyncDisposable, disposedObjects); | ||
} | ||
} | ||
|
||
public class AsyncDisposable : IAsyncDisposable | ||
{ | ||
private readonly Action<object> onDisposed; | ||
|
||
public AsyncDisposable(Action<object> onDisposed) | ||
{ | ||
this.onDisposed = onDisposed; | ||
} | ||
public ValueTask DisposeAsync() | ||
{ | ||
onDisposed(this); | ||
return ValueTask.CompletedTask; | ||
} | ||
} | ||
|
||
public class Disposable : IDisposable | ||
{ | ||
private readonly Action<object> onDisposed; | ||
|
||
public Disposable(Action<object> onDisposed) | ||
{ | ||
this.onDisposed = onDisposed; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
onDisposed(this); | ||
} | ||
} | ||
} |
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