Skip to content

JSInterop error on circuit disposal in .NET 6 #38

@smfields

Description

@smfields

Describe the bug
An exception is thrown during the Dispose handler of the IntersectionObserve component when the circuit itself is being disposed.

warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
      Unhandled exception rendering component: JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.
      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)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
         at Ljbc1994.Blazor.IntersectionObserver.IntersectionObserverService.RemoveObserver(String id)
         at Ljbc1994.Blazor.IntersectionObserver.IntersectionObserver.Dispose()
         at Ljbc1994.Blazor.IntersectionObserver.Components.IntersectionObserve.DisposeAsync()
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.<>c__DisplayClass69_0.<<Dispose>g__HandleAsyncExceptions|1>d.MoveNext()

Everything still works fine after the exception, but it fills up the error log with unnecessary error messages.

To Reproduce

  1. Create a page that contains the IntersectionObserve component
  2. Refresh or close the page
  3. An exception will be logged to the error logs

Expected behavior
Catch and silently ignore the exception since we don't need to dispose of the observer if the browser is being closed anyway.

System Info

  • Blazor Server
  • .NET 6

Additional context
This was caused by a changed to Blazor that prevents JS Interop actions during circuit disposal: dotnet/aspnetcore#32901

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions