Skip to content
16 changes: 8 additions & 8 deletions src/Components/Components/src/OwningComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected IServiceProvider ScopedServices
}
}

/// <inhertidoc />
/// <inheritdoc />
void IDisposable.Dispose()
{
Dispose(disposing: true);
Expand All @@ -69,12 +69,14 @@ protected virtual void Dispose(bool disposing)
}
}

/// <inhertidoc />
/// <inheritdoc />
async ValueTask IAsyncDisposable.DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);

Dispose(disposing: false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the fix just be Dispose(disposing: true) here?

if (!IsDisposed)
{
await DisposeAsyncCore().ConfigureAwait(false);
Dispose(disposing: true);
}
GC.SuppressFinalize(this);
}

Expand All @@ -84,13 +86,11 @@ async ValueTask IAsyncDisposable.DisposeAsync()
/// <returns>A task that represents the asynchronous dispose operation.</returns>
protected virtual async ValueTask DisposeAsyncCore()
{
if (!IsDisposed && _scope.HasValue)
if (_scope.HasValue)
{
await _scope.Value.DisposeAsync().ConfigureAwait(false);
_scope = null;
}

IsDisposed = true;
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Components/Components/test/OwningComponentBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ public MyService(Counter counter)
void IDisposable.Dispose() => Counter.DisposedCount++;
}

[Fact]
public async Task DisposeAsync_CallsDispose_WithDisposingTrue()
{
var services = new ServiceCollection();
services.AddSingleton<Counter>();
services.AddTransient<MyService>();
var serviceProvider = services.BuildServiceProvider();

var renderer = new TestRenderer(serviceProvider);
var component = (ComponentWithDispose)renderer.InstantiateComponent<ComponentWithDispose>();

_ = component.MyService;
await ((IAsyncDisposable)component).DisposeAsync();
Assert.True(component.DisposingParameter);
}

private class ComponentWithDispose : OwningComponentBase<MyService>
{
public MyService MyService => Service;
public bool? DisposingParameter { get; private set; }

protected override void Dispose(bool disposing)
{
DisposingParameter = disposing;
base.Dispose(disposing);
}
}

private class MyOwningComponent : OwningComponentBase<MyService>
{
public MyService MyService => Service;
Expand Down
Loading