Skip to content

Blazor: #32901 breaks a JS invocation from the IAsyncDisposable.DisposeAsync method #33336

@pos777

Description

@pos777

Hello,

It looks like the #32901 fix breaks an existing blazor component.
The issue is related to #32901 (comment).
In .NET 5 I have to handle the TaskCanceledException exception when invoke any JS inside the IAsyncDisposable.DisposeAsync to process component's disposing when a browser page is closed (https://github.com/pos777/Blazor_DisposeAsync_Sample/blob/632378bc60e8e1930b73a273c417f61458018b62/NET5/NET5/Shared/TestComponent.razor#L13):

async ValueTask IAsyncDisposable.DisposeAsync()
{
    try
    {
        await JS.InvokeVoidAsync("console.log", "TestComponent.DisposeAsync");
    }
    catch (TaskCanceledException) { }
}

This code does not work under .NET 6. The JSDisconnectedException exception occurs in .NET 6.
Since the JSDisconnectedException is a new class and it is not available in .NET 5, I cannot handle it in .NET 5.
I need to keep using the net5.0 target in my component library project. But it should also work in .NET 6. Please advise, how it is supposed to process the scenario?

To Reproduce

.NET 5:

.NET 6:

Exceptions

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'DTYIfmJRu2-bAa8jKA_vxbF6NESzfXcAYlgt5ZPDRgY'.
      Microsoft.JSInterop.JSDisconnectedException: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.
         at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId) in Microsoft.AspNetCore.Components.Server.dll:token 0x6000245+0x1a
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args) inMicrosoft.JSInterop.dll:token 0x600003a+0xda
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args) in Microsoft.JSInterop.dll:token 0x6000039+0x48
         at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args) in Microsoft.JSInterop.dll:token 0x6000046+0x82
         at NET6.Shared.TestComponent.System.IAsyncDisposable.DisposeAsync() ind:\NET6\NET6\Shared\TestComponent.razor:line 11
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.<Dispose>g__HandleAsyncExceptions|62_0(List`1 tasks) in Microsoft.AspNetCore.Components.dll:token 0x6000248+0x91

Further technical details

C:\>dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-preview.6.21306.2
 Commit:    c83bef2f34

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.3.9600
 OS Platform: Windows
 RID:         win81-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-preview.6.21306.2\

Host (useful for support):
  Version: 6.0.0-preview.6.21306.1
  Commit:  5b8e1780ad

.NET SDKs installed:
  2.1.524 [C:\Program Files\dotnet\sdk]
  2.1.815 [C:\Program Files\dotnet\sdk]
  2.2.108 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]
  3.1.408 [C:\Program Files\dotnet\sdk]
  5.0.300 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.6.21306.2 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.6.21306.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.6.21306.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.6.21305.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Thanks,
Ilya

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions