WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed#66337
Draft
WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed#66337
Conversation
…isposed Add disconnection guard to WebViewJSRuntime, matching the existing behavior in RemoteJSRuntime for Blazor Server circuits. When a BlazorWebView page reloads, the old PageContext is disposed and components implementing IAsyncDisposable that invoke JS interop during disposal will now see JSDisconnectedException instead of an unhandled JSException. Changes: - WebViewJSRuntime: Add IsDisposed flag and MarkAsDisconnected() method; throw JSDisconnectedException in BeginInvokeJS; silently return in EndInvokeDotNet/SendByteArray when disposed. - PageContext: Call JSRuntime.MarkAsDisconnected() before Renderer.DisposeAsync() (matching CircuitHost.DisposeAsync pattern). - IpcReceiver: Ignore incoming messages for disposed page contexts. - Add test verifying JSDisconnectedException during component dispose on reload. Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/e322fe08-acb0-476a-82e1-724f17da8728 Co-authored-by: oroztocil <79744616+oroztocil@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/e322fe08-acb0-476a-82e1-724f17da8728 Co-authored-by: oroztocil <79744616+oroztocil@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/e322fe08-acb0-476a-82e1-724f17da8728 Co-authored-by: oroztocil <79744616+oroztocil@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix WebViewJSRuntime to throw JSDisconnectedException on PageContext disposal
WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed
Apr 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WebViewJSRuntime: throw JSDisconnectedException when PageContext is disposed
Parity with RemoteJSRuntime: throw JSDisconnectedException instead of JSException when components invoke JS interop during disposal of a reloading WebView page.
Description
When a BlazorWebView page reloads, the old
PageContextis disposed while components implementingIAsyncDisposablemay still invoke JS interop for cleanup. These calls hit the reloaded page's JS context where old object IDs no longer exist, producing unhandledJSException.RemoteJSRuntime(Blazor Server) already handles this by throwingJSDisconnectedExceptionwhen the circuit is disposed —WebViewJSRuntimehad no equivalent guard.Changes
WebViewJSRuntime— Add_isDisposedflag withMarkAsDisconnected().BeginInvokeJSthrowsJSDisconnectedExceptionwhen disposed;EndInvokeDotNet/SendByteArraysilently no-op.PageContext.DisposeAsync— CallJSRuntime.MarkAsDisconnected()beforeRenderer.DisposeAsync(), matching theCircuitHost.DisposeAsyncpattern.IpcReceiver— Ignore incoming messages for a disposed page context to prevent stale messages from corrupting new page state.JSObjectReference.DisposeAsyncalready catchesJSDisconnectedException(#49418), so this completes the fix end-to-end.