Skip to content

Commit

Permalink
Dispose DI scope with async support in circuit host
Browse files Browse the repository at this point in the history
Fixes part of #12918

This fixes the part of this issue that we're going to be able to do in
3.0 safely.
  • Loading branch information
rynowak committed Aug 14, 2019
1 parent e5a950d commit 13fc89c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Components/Server/src/Circuits/CircuitHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,18 @@ await Renderer.Dispatcher.InvokeAsync(async () =>
try
{
Renderer.Dispose();
_scope.Dispose();
// This cast is needed because it's possible the scope may not support async dispose.
// Our DI container does, but other DI systems may not.
if (_scope is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else
{
_scope.Dispose();
}
Log.DisposeSucceeded(_logger, CircuitId);
}
catch (Exception ex)
Expand Down
27 changes: 27 additions & 0 deletions src/Components/Server/test/Circuits/CircuitHostTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ public async Task DisposeAsync_DisposesResources()
Assert.Null(circuitHost.Handle.CircuitHost);
}

[Fact]
public async Task DisposeAsync_DisposesScopeAsynchronouslyIfPossible()
{
// Arrange
var serviceScope = new Mock<IServiceScope>();
serviceScope
.As<IAsyncDisposable>()
.Setup(f => f.DisposeAsync())
.Returns(new ValueTask(Task.CompletedTask))
.Verifiable();

var remoteRenderer = GetRemoteRenderer();
var circuitHost = TestCircuitHost.Create(
Guid.NewGuid().ToString(),
serviceScope.Object,
remoteRenderer);

// Act
await circuitHost.DisposeAsync();

// Assert
serviceScope.Verify(s => s.Dispose(), Times.Never());
serviceScope.As<IAsyncDisposable>().Verify(s => s.DisposeAsync(), Times.Once());
Assert.True(remoteRenderer.Disposed);
Assert.Null(circuitHost.Handle.CircuitHost);
}

[Fact]
public async Task DisposeAsync_DisposesResourcesAndSilencesException()
{
Expand Down

0 comments on commit 13fc89c

Please sign in to comment.