Skip to content

COMException when removing event handler from CoreWebView2 #338

@fvanheeswijk

Description

@fvanheeswijk

We are facing an issue in our application when we remove event handlers from the CoreWebView2, we need to explicitly remove them in our actual application because we need to update certain bookkeeping.

Upon removal of the event handler we get a COMException however:

System.Runtime.InteropServices.COMException (0x8007139F): The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2.remove_NewWindowRequested(EventRegistrationToken token)
at Microsoft.Web.WebView2.Core.CoreWebView2.remove_NewWindowRequested(EventHandler`1 value) Error code App:90001

We have managed to create the following reproducible example:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;

namespace WebView2EventHandler
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(CreateNewForm());
        }

        private static Form CreateNewForm()
        {
            var form = new Form
            {
                Size = new Size(800, 600)
            };

            var button = new Button
            {
                Dock = DockStyle.Fill,
                Text = "Open form"
            };
            button.Click += (sender, e) =>
            {
                EventHandler<CoreWebView2NewWindowRequestedEventArgs> newWindowRequestedEvent = (sender2, e2) =>
                {
                    e2.Handled = false;
                };

                var webView2 = new WebView2
                {
                    Dock = DockStyle.Fill,
                    Source = new Uri(@"about:blank")
                };
                CoreWebView2 coreWebView2 = null;
                webView2.CoreWebView2Ready += (sender2, e2) =>
                {
                    coreWebView2 = webView2.CoreWebView2;
                    coreWebView2.NewWindowRequested += newWindowRequestedEvent;
                    coreWebView2.Navigate(@"https://www.google.com");

                };

                var modalForm = new Form
                {
                    Size = new Size(1280, 1024)
                };
                modalForm.Controls.Add(webView2);

                modalForm.ShowDialog(form);

                modalForm.Dispose();
                coreWebView2.NewWindowRequested -= newWindowRequestedEvent;
            };

            form.Controls.Add(button);

            return form;
        }
    }
}

We expect to be able to remove the event handlers explicitly. A difference we have encountered of your WebView2 versus other components is that other components actually have two parts: The WinForms control and the webview itself, in those components we have never encountered similar issues so maybe a split could be helpful.

AB#27803586

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions