-
Notifications
You must be signed in to change notification settings - Fork 64
Description
What happened?
Description
When a WebView2 control is disposed (via Dispose() or when the host application closes), it does not fire the standard browser window/tab close events that web applications rely on for cleanup.
Expected Behavior
When WebView2 is disposed, it should fire the following events in the web content before terminating:
beforeunloadeventpagehideeventunloadeventvisibilitychangeevent (withdocument.visibilityState = 'hidden')
This matches the behavior of Chrome/Edge when closing a tab or window.
Actual Behavior
WebView2 immediately terminates without firing these events, leaving web applications unable to perform cleanup operations such as:
- Closing WebSocket/SignalR connections gracefully
- Saving state
- Sending analytics/telemetry
- Clearing timers/intervals
Impact
This particularly affects Blazor Server applications which rely on these events to disconnect circuits. Without them:
- Circuits remain active for 30+ seconds until timeout
- Server resources are held unnecessarily
- Connection state becomes inconsistent
Reproduction Steps
- Create a Blazor Server application
- Host it in a WinForms WebView2 control
- Add event listeners for
beforeunload,pagehide,unloadin JavaScript - Close the WinForms application or call
webView2Control.Dispose() - Observe that the events are never fired
Current Workaround
Manually inject script to fire these events before disposal:
Importance
Moderate. My app's user experience is affected, but still usable.
Runtime Channel
Stable release (WebView2 Runtime)
Runtime Version
142.0.3595.80
SDK Version
1.0.3595.46
Framework
Winforms
Operating System
Windows 11
OS Version
No response
Repro steps
repo:
https://github.com/zuizuihao/TestBlazorPageInsideWebview2
only works when I add this fix: Add WebView2 cleanup logic on form closing

remove this fix, can reproduce this issue with below steps.
steps:
open blazor page with webview2:

in blazor server console, no circuit disconnect shows.

Repros in Edge Browser
Yes, issue can be reproduced in the corresponding Edge version
Regression
No, this never worked
Last working version (if regression)
No response