MicrosoftEdge / MicrosoftEdge/WebView2Feedback

CoreWebView2 members cannot be accessed after the WebView2 control is disposed Exception in CoreWebView2.Reload

Open
#2,872 17 comments 0 reactions 2 assignees View on GitHub

@LiangTheDev is already working on this.

Since Mar 18, 2024.

bug tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description
The bug is sporadic on several machines.
It seems that it is tied to the simultaneous initialization of several WebView2... Not true - it happens with only one form too.

UnhandledException details: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: CoreWebView2 members cannot be accessed after the WebView2 control is disposed.
 ---> System.Runtime.InteropServices.COMException (0x8007139F): The group or resource is not in the correct state to perform the requested operation. (0x8007139F)
   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2.Reload()
   at Microsoft.Web.WebView2.Core.CoreWebView2.Reload()

Version
SDK: 1.0.1343.22, 1.0.1370.28
Runtime: 106.0.1370.34 and 106.0.1370.37
Framework: WinForms .Net6
Windows 10 Pro (Microsoft Windows 10.0.19043) and Windows 7 Pro x64 (6.1.7601 Service Pack 1).

Repro Steps

Unfortunately, I could not make a 100% repro app :( But core code like this:

var envO = new Microsoft.Web.WebView2.Core.CoreWebView2EnvironmentOptions() { AdditionalBrowserArguments = "--disable-features=msWebOOUI,msPdfOOUI" };
_webView2.CoreWebView2InitializationCompleted += _webView2_CoreWebView2Ready;
var env = await CoreWebView2Environment.CreateAsync(options: envO).ConfigureAwait(true);
await _webView2.EnsureCoreWebView2Async(env).ConfigureAwait(true);
if (_webView2.CoreWebView2 is not null)
{
    await _webView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("document.addEventListener('keydown',event => { if (event.isComposing || event.keyCode === 229) return; " +
        "if(event.ctrlKey && event.code === 'KeyG') window.chrome.webview.postMessage('CtrlG'); " +
        "else if (event.ctrlKey && event.code === 'KeyD') window.chrome.webview.postMessage('CtrlD');" +
        "else if (event.ctrlKey && event.code === 'KeyS') window.chrome.webview.postMessage('CtrlS');});" +
        "document.addEventListener('dblclick', event => { window.chrome.webview.postMessage('dblclick');});").ConfigureAwait(true);
    _webView2.Reload(); // Exception here
}

private void _webView2_CoreWebView2Ready(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
    if (!e.IsSuccess)
    {
        //_Logger.Error(e.InitializationException, "WebView2 Initialization Exception");
        return;
    }

    //_Logger.Info($"WebView2 ready. RuntimeVersion {_webView2.CoreWebView2.Environment.BrowserVersionString}; UserDataFolder {_webView2.CoreWebView2.Environment.UserDataFolder}.");
    _webView2.CoreWebView2.Environment.NewBrowserVersionAvailable += Environment_NewBrowserVersionAvailable;
    _webView2.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed;
    _webView2.CoreWebView2.ContextMenuRequested += CoreWebView2_ContextMenuRequested;
}

Additional context
No ProcessFailed before this exception.
Similar issues I found:
https://github.com/MicrosoftEdge/WebView2Feedback/issues/2302
https://github.com/MicrosoftEdge/WebView2Feedback/issues/2311
https://github.com/MicrosoftEdge/WebView2Feedback/issues/2398
https://github.com/MicrosoftEdge/WebView2Feedback/issues/2631

AB#49560812

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.