MicrosoftEdge / MicrosoftEdge/WebView2Feedback

CoreWebView2.AddHostObjectToScript throws exception "The group or resource is not in the correct state to perform the requested operation."

Open
#2,754 4 comments 1 reaction 1 assignee View on GitHub

@david-risney is already working on this.

Since Sep 8, 2022.

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

Description

Description
I'm trying to add a host object into a WebView2, but I'm getting the following exception:

The group or resource is not in the correct state to perform the requested operation.

Version
SDK: Microsoft.Web.WebView2 version 1.0.1264.42
Runtime: 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27
Framework: UWP
OS: Win10 version 10.0.19043 Build 19043

Repro Steps

This is the code that I'm running:

[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Example
{
    public string Prop { get; set; } = "example";
}

namespace Example_UWP
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
            InitializeAsync();
        }

        public async Task InitializeAsync()
        {
            await ExampleView.EnsureCoreWebView2Async();

            ExampleView.Source = new Uri("http://localhost:3000");
            ExampleView.CoreWebView2.OpenDevToolsWindow();

            ExampleView.CoreWebView2.AddHostObjectToScript("example", new Example());
        }
    }
}

After the exception, the host object is not available in chrome.webview.hostObjects or chrome.webview.hostObjects.sync.

I've tried different potential solutions such as:

  • Keeping a reference to the Example instance in an attribute inside Example_UWP to avoid potential GC
  • Adding the host object before and after each of the previous steps within InitializeAsync
  • Wait for the event NavigationCompleted to add the host object.
  • Wait for 5 seconds before adding the host object.

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.