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() results in exception #3680

Closed
ray007 opened this issue Aug 2, 2023 · 9 comments
Closed

AddHostObjectToScript() results in exception #3680

ray007 opened this issue Aug 2, 2023 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@ray007
Copy link

ray007 commented Aug 2, 2023

Description
I'm calling

CoreWebView2.AddHostObjectToScript("name", new Bridge(view));

and get an exception: System.ArgumentException: "Value does not fall within the expected range."

Stacktrace into webview:

   at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2.AddHostObjectToScript(String name, Object& object)
   at Microsoft.Web.WebView2.Core.CoreWebView2.AddHostObjectToScript(String name, Object rawObject)
   at Out.Company.Presenter.<InitializeWebviewAsync>d__16.MoveNext() in C:\rtc-sandbox\...\Presenter.cs:line 290

Version
SDK: 1.0.1901.177
Runtime: 115.0.1901.183
Framework: WPF with .NET 4.7.2
OS: Win10

Regression
Was this working before but has regressed?
Unknown since I had no old version.

Additional context
The Bridge class is defined with attributes

	[ClassInterface(ClassInterfaceType.AutoDual)]
	[ComVisible(true)]

is there something else I need to do take make the Bridge usable as host object?

@ray007 ray007 added the bug Something isn't working label Aug 2, 2023
@sq800
Copy link

sq800 commented Aug 29, 2023

I had this problem too, but I finally solved it, this is my problem

var libraryHelper = new LibraryHelper();
webView.CoreWebView2.AddHostObjectToScript("LibraryHelper", libraryHelper); //System.ArgumentException:“Value does not fall within the expected range.”

Version
SDK: 1.0.1901.177
Runtime: 116.0.1938.162
Framework: WPF with .NET 6
OS: Win11

I found the cause of the exception. My "LibraryHelper" class forgot to add modifier. After adding the "Public" modifier, everything was normal.

Modify

image

To

image

@ray007
Copy link
Author

ray007 commented Aug 29, 2023

@sq800 Thanks for trying, but unfortunately, that didn't change anything for me.

@ray007
Copy link
Author

ray007 commented Sep 13, 2023

Hmm, some more experimenting and upgrading Microsoft.Web.WebView2 to version 1.0.2065-prerelease, things do work a bit better now.
I now do not get an exception on calling AddHostObjectToScript() anymore, but in many cases when invoking the methods of the host object.

There seems to be a problem with optional parameters. They get filled in correctly, but I get a System.Runtime.InteropServices.COMException on (or after) the method return when I do not provide all of them in the call:

   at Microsoft.Web.WebView2.Core.CoreWebView2PrivateHostObjectHelper.RawHelper.IsAsyncMethod(Object& rawObject, String methodName, Int32 parameterCount)

But the value still gets returned to the webview.

At least simple values.
I have yet to find a way to hava javascript objects as parameters or result of a method call.
Other than JSON serialize/deserialize.
I tried with Hashtable and Dictionary<String, Object>, but no luck so far. Is there a type for that?

@ray007
Copy link
Author

ray007 commented Sep 25, 2023

Edit: some mistakes on my part yesterday, other than objects as parameters or call-result, things seem to be working now.

@victorhuangwq
Copy link
Collaborator

@ray007 I would close this issue if it's not an issue?

@ray007
Copy link
Author

ray007 commented Nov 21, 2023

Did I miss something about parameters of type Object?
But all right, I'll close it.

@ray007 ray007 closed this as completed Nov 21, 2023
@jMWCQQQ1
Copy link

jMWCQQQ1 commented Jul 3, 2024

Hmm, some more experimenting and upgrading Microsoft.Web.WebView2 to version 1.0.2065-prerelease, things do work a bit better now. I now do not get an exception on calling AddHostObjectToScript() anymore, but in many cases when invoking the methods of the host object.

There seems to be a problem with optional parameters. They get filled in correctly, but I get a System.Runtime.InteropServices.COMException on (or after) the method return when I do not provide all of them in the call:

   at Microsoft.Web.WebView2.Core.CoreWebView2PrivateHostObjectHelper.RawHelper.IsAsyncMethod(Object& rawObject, String methodName, Int32 parameterCount)

But the value still gets returned to the webview.

At least simple values. I have yet to find a way to hava javascript objects as parameters or result of a method call. Other than JSON serialize/deserialize. I tried with Hashtable and Dictionary<String, Object>, but no luck so far. Is there a type for that?

i have a same exception. i don't know why the IsAsyncMethod called. How did you solve this exception

@ray007
Copy link
Author

ray007 commented Jul 3, 2024

@jMWCQQQ1 make sure it's declard as public class and I have some more lines before the class declaration:

#pragma warning disable CS0618
	[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
	[ComVisible(true)]

I think that's all I did to make it work back then...

@jMWCQQQ1
Copy link

jMWCQQQ1 commented Jul 4, 2024

@jMWCQQQ1 make sure it's declard as public class and I have some more lines before the class declaration:

#pragma warning disable CS0618
	[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
	[ComVisible(true)]

I think that's all I did to make it work back then...

I have already done these same things, but still report errors。but thanks

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

No branches or pull requests

6 participants
@ray007 @victorhuangwq @yunate @sq800 @jMWCQQQ1 and others