Host Object returning multidimensional array with null.
Hello everyone, when I used hostobject what return:
public object[] Foo() { return new object[] { 1, 2, "foo", true, false} }
In Javascript I receive:
[1, 2, "foo", true, false]
But when I call a method like this:
public object[] Foo2() { return new object[] { 1, 2, "foo", true, false, new object[] { 5, 6, "foo" } } }
In Javascript I receive:
[1, 2, "foo", true, false, [null, null, null] ]
is that a hostobject limitation?
Thanks!.
I found another bug?
From JS to C#, I do this:
window.chrome.webview.hostObjects.sync.MyObject.Foo( [1, 2, 3, "foo", [] ] )
In C# I receive
{ 1, 2, 3, "foo", {} } //All ok here
But when I try this in JS
window.chrome.webview.hostObjects.sync.MyObject.Foo( [1, 2, 3, "foo", [5] ] )
in Devtools console throw me this exception:
Uncaught Error: El par�metro no es correcto. (0x80070057)
at RemoteMessenger.postSyncRequestMessage (
Hi @erovas,
Thanks for your report! Would you please give a bit more info:
Versions
- What version of the SDK are you using?
- What is the version of the runtime where you see this?
Usage
- Are you using the wv2winrt tool or just .NET IDispatch objects?
- Does your code/feature/project depend on support for nested arrays of arbitrary objects? Arrays of different types (including potentially more arrays of different types) seems difficult to use, we'd like to understand your scenario.
Thanks, Luis
Hi @lflores-ms,
I am using the last released nuget package:
- Microsoft.Web.WebView2 (1.0.1518.46)
In a WPF application on .NET 6.0, following examples.
Yes, my project require allow pass nested arrays between JS to C#, and viceversa
My simple code to reproduce:
public class MyHO
{
public void FooIn (string name, object[] asd)
{
var ArrObj = asd;
}
public object[] FooOut()
{
return new object[] { 1, 2, "foo", true, false, new object[] { 5, 6, "foo" } };
}
}
I add to Webview:
public partial class MyWPF : Window
{
//.......
protected async override OnSourceInitialized (EventArgs e)
{
base.OnSourceInitialized(e);
//...
MyHO myho = new MyHO();
this.WV2.CoreWebView2.AddHostObjectToScript("MyObject", myho);
//....
}
}
In JS I try for example this:
window.chrome.webview.hostObjects.sync.MyObject.FooOut()
I found another bug?
From JS to C#, I do this:
window.chrome.webview.hostObjects.sync.MyObject.Foo( [1, 2, 3, "foo", [] ] )
In C# I receive
{ 1, 2, 3, "foo", {} } //All ok here
But when I try this in JS
window.chrome.webview.hostObjects.sync.MyObject.Foo( [1, 2, 3, "foo", [5] ] )
in Devtools console throw me this exception:
Uncaught Error: El par�metro no es correcto. (0x80070057) at RemoteMessenger.postSyncRequestMessage (:1:16147) at Function.applyHostFunction (:1:32485) at Object.apply (:1:22382) ...
My bad, I was in a error. I was sending from JS to C# this data
window.chrome.webview.hostObjects.sync.MyObject.FooIn("nombre", [1, ["some", 1, 2, [6, 7, 8] ] ] )
But acording to HostObject limitations:
Nested arrays are supported up to a depth of 3. Arrays of by reference types are not supported
That is possible, because when I try this
window.chrome.webview.hostObjects.sync.MyObject.FooIn("nombre", [1, ["some", 1, 2, [] ] ] )
I receive in C#:
name == "nombre"
asd == {1, { "some", 1, 2, {} } }
@erovas just to clarify, are you saying that the second issue is actually working as expected?
I have opened a bug on our end for the first issue. We will update here when/if this gets addressed.
@lflores-ms Hi! To enumerate issues:
-
- https://github.com/MicrosoftEdge/WebView2Feedback/issues/3183#issue-1568716328
-
- https://github.com/MicrosoftEdge/WebView2Feedback/issues/3183#issuecomment-1421507927
To clarify second issue:
I try from JS:
window.chrome.webview.hostObjects.sync.MyObject.FooIn("nombre", [1, ["some", 1, 2, [6, 7, 8] ] ] )
in Devtools console throw me this error:
Uncaught Error: El par�metro no es correcto. (0x80070057)
at RemoteMessenger.postSyncRequestMessage (:1:16147)
at Function.applyHostFunction (:1:32485)
at Object.apply (:1:22382)
...
but, when I try
window.chrome.webview.hostObjects.sync.MyObject.FooIn("nombre", [1, ["some", 1, 2, [] ] ] )
I receive in C# this
name == "nombre"
asd == {1, { "some", 1, 2, {} } }
Thanks a lot for your attention!
Hello, any news about this?
Hi, @lflores-ms , the bug is still there with latest WebView2 1.0.3537.50
Pretty easy to reproduce, just use a .NET Winforms project, create a host object like this:
public class HostObject
{
public object GetArrayOk()
{
return new object[] { 1, 2, 3, 4, 5 };
}
public object GetArrayNotOk()
{
return new object[] { new object[] { 1, 2, 3, 4, 5 }, new object[] { 6, 7, 8, 9, 0 } };
}
}
From Javascript
const array1 = chrome.webview.hostObjects.sync.dotnet.GetArrayOk()
works fine
while
const array2 = chrome.webview.hostObjects.sync.dotnet.GetArrayNotOk();
does report an array of 2 elements, each element being itself an array of 5 elements, but all items of these arrays are null.
AddHostObjectToScript Documentation clearly states that "Nested arrays are supported up to a depth of 3". Is there any workaround or option to set somewhere?