Skip to content

Blazor JS Interop page gives example of Dispose pattern which causes unhandled exceptions. #34126

@willdean

Description

@willdean

Description

The Blazor JS Interop page contains the following example of disposing a JS Module:

  async ValueTask IAsyncDisposable.DisposeAsync()
    {
        if (module is not null)
        {
            await module.DisposeAsync();
        }
    }

However, this can lead to an unhandled exception being thrown (noticeable in the debugger, even if it's silent elsewhere).

This has been reported before: In this issue dotnet/aspnetcore#38822 Steve Sanderson said:

This is as designed. The exception here is JSDisconnectedException, indicating that the JS interop call cannot be issued because there's no active connection. In this case you probably want to ignore such exceptions, so you should use a try/catch for that specific exception type in this place.

To me, this implies that the code example on the doc page should look more like this:

    async ValueTask IAsyncDisposable.DisposeAsync()
    {
        if (module is not null)
        {
            try
            {
                await module.DisposeAsync();
            }
            catch (JSDisconnectedException)
            {
            }
        }
    }

I'd imagine that it's not the intention that example code should throw unhandled exceptions.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-9.0&preserve-view=true

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/javascript-interoperability/index.md

Document ID

49fa31d8-72a6-01a1-027d-6edeac9e0fc4

Article author

@guardrex

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions