JSInterop error on circuit disposal in .NET 6
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
- Create a page that contains the IntersectionObserve component
- Refresh or close the page
- 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: https://github.com/dotnet/aspnetcore/pull/32901
Hi @smfields,
Will look into this when I've got the chance 👍 Thanks for the detailed report this is very helpful
@ljbc1994 Any chance, that a fix for this will come? We have the same problem and the only thing we can currently do, is suppressing the log by ugly Exception filtering.