Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddHostObjectToScript - Caching chrome.webview.hostObjects.external in a variable causes Error: Element not found. (0x80070490) #525

Closed
dorofino opened this issue Oct 12, 2020 · 6 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@dorofino
Copy link

dorofino commented Oct 12, 2020

Hi,

I'm using the following code to call a method from Javascript

using System.Diagnostics;
using System.Runtime.InteropServices;

        private bool _hostObjectAdded = false;
        private void AddHostObject()
        {
			if (!_hostObjectAdded)
			{
				_hostObjectAdded = true;
				webView.CoreWebView2.AddHostObjectToScript("external", new BrowserHostObject());
			}
		}

        async void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
        {
            _isNavigating = true;
            RequeryCommands();

            await webView.EnsureCoreWebView2Async();
            AddHostObject();
        }

	[ClassInterface(ClassInterfaceType.AutoDual)]
	[ComVisible(true)]
	public class BrowserHostObject
	{ 
		public bool Test(string something)
		{
			string message = $"***Test({something}) Called***";
			Debug.Print(message);
			return true;
		}
	} 

The JavaScript method looks like this

 <button onclick="JsTest()">Test Call</button>
<script>
        const external = chrome.webview.hostObjects.external;
        function JsTest() {
                external.Test('This is a test');
        }
</script>

After a couple of clicks and randomly I get the following error.

Error: Element not found. (0x80070490)

It works again when I right-click on the page and reload the content on the WebView2, without restarting the program.
Does anyone know why this happens?

AB#28271329

@dorofino dorofino added the bug Something isn't working label Oct 12, 2020
@champnic
Copy link
Member

I believe this is because you are caching the 'external' object in JS, and I think this has caused some garbage collection related issues in the past (or something similar). Can you try using the full path to call?
chrome.webview.hostObjects.external.Test('This is a test');

Let me know if that fixes it for you. I've opened a bug on our end to look into this. Thanks!

@champnic champnic added the tracked We are tracking this work internally. label Oct 12, 2020
@dorofino
Copy link
Author

It looks like it works!
You are a genius!

I believe this is because you are caching the 'external' object in JS, and I think this has caused some garbage collection related issues in the past (or something similar). Can you try using the full path to call?
chrome.webview.hostObjects.external.Test('This is a test');

Let me know if that fixes it for you. I've opened a bug on our end to look into this. Thanks!

@champnic champnic changed the title WPF Sample + AddHostObjectToScript - Error: Element not found. (0x80070490) AddHostObjectToScript - Caching chrome.webview.hostObjects.external in a variable causes Error: Element not found. (0x80070490) Oct 16, 2020
@pontusn
Copy link

pontusn commented Oct 22, 2020

We also experience limited life-time for proxies. Sofar we have implemented workarounds by using full-path access. For entities in our model we cache IDs and use lookup functions. And in few situations where the COM object is only referred from JS we had to publish references to sn external collection implemented in C++.

@pagoe-msft
Copy link

Hi Everyone,

This was addressed in the 1.0.707-prerelease version.

@DexterWoo
Copy link

we recently saw a similar error in our project.
the same thing is we also created a js proxy object to hold the full-path injected object.
is that really fixed in the webview2 release after 1.0.707-prerelease? we are using 1.0.1293.44 now

@pontusn
Copy link

pontusn commented Aug 31, 2022

Regression problems may occure, but we did not experience any lifetime issues in our application since the fix was introduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

5 participants