Skip to content

Commit

Permalink
Merge pull request #583 from seesharper/call-completed-handler-from-d…
Browse files Browse the repository at this point in the history
…isposeasync

Call Completed handler on Dispose and DisposeAsync
  • Loading branch information
seesharper committed Dec 31, 2022
2 parents 13624c8 + 2826eb9 commit 3bde539
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/LightInject.Tests/AsyncDisposableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ namespace LightInject.Tests
{
public class AsyncDisposableTests : TestBase
{
[Fact]
public async Task ShouldCallCompletedHandlerOnDisposeAsync()
{
var container = CreateContainer();
List<object> disposedObjects = new();

container.RegisterScoped<AsyncDisposable>(sf => new AsyncDisposable(disposedObject => disposedObjects.Add(disposedObject)));

AsyncDisposable asyncDisposable = null;
bool hasCompleted = false;
await using (var scope = container.BeginScope())
{
scope.Completed += (o, e) => hasCompleted = true;
asyncDisposable = container.GetInstance<AsyncDisposable>();
}

Assert.Contains(asyncDisposable, disposedObjects);
Assert.True(hasCompleted);
}

[Fact]
public async Task ShouldDisposeAsyncDisposable()
{
Expand All @@ -17,6 +37,7 @@ public async Task ShouldDisposeAsyncDisposable()
container.RegisterScoped<AsyncDisposable>(sf => new AsyncDisposable(disposedObject => disposedObjects.Add(disposedObject)));

AsyncDisposable asyncDisposable = null;

await using (var scope = container.BeginScope())
{
asyncDisposable = container.GetInstance<AsyncDisposable>();
Expand Down
17 changes: 12 additions & 5 deletions src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6724,10 +6724,7 @@ public void Dispose()
}
}

scopeManager?.EndScope(this);
var completedHandler = Completed;
completedHandler?.Invoke(this, new EventArgs());
IsDisposed = true;
EndScope();
}
#if USE_ASYNCDISPOSABLE
/// <inheritdoc/>
Expand Down Expand Up @@ -6771,6 +6768,8 @@ public ValueTask DisposeAsync()
}
}
}

EndScope();
return default;

static async ValueTask Await(int i, ValueTask vt, List<object> toDispose, HashSet<object> disposedObjects)
Expand Down Expand Up @@ -6800,7 +6799,15 @@ static async ValueTask Await(int i, ValueTask vt, List<object> toDispose, HashSe
}
}
#endif
/// <inheritdoc/>
private void EndScope()
{
scopeManager?.EndScope(this);
var completedHandler = Completed;
completedHandler?.Invoke(this, new EventArgs());
IsDisposed = true;
}

/// <inheritdoc/>
public Scope BeginScope() => serviceFactory.BeginScope();

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/LightInject/LightInject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;netcoreapp3.1;net462</TargetFrameworks>
<Version>6.6.2</Version>
<Version>6.6.3</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://www.lightinject.net</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand Down

0 comments on commit 3bde539

Please sign in to comment.