WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Host Object returning multidimensional array with null.

Open erovas opened this issue 2 years ago • 8 comments

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!.

AB#43258454

erovas avatar Feb 02 '23 20:02 erovas

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) ...

erovas avatar Feb 06 '23 20:02 erovas

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

lflores-ms avatar Feb 06 '23 22:02 lflores-ms

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()

erovas avatar Feb 07 '23 13:02 erovas

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 avatar Feb 07 '23 21:02 erovas

@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 avatar Feb 13 '23 10:02 lflores-ms

@lflores-ms Hi! To enumerate issues:

    1. https://github.com/MicrosoftEdge/WebView2Feedback/issues/3183#issue-1568716328
    1. 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!

erovas avatar Feb 13 '23 11:02 erovas

Hello, any news about this?

erovas avatar May 12 '25 20:05 erovas

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

Image

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.

Image

AddHostObjectToScript Documentation clearly states that "Nested arrays are supported up to a depth of 3". Is there any workaround or option to set somewhere?

smourier avatar Oct 22 '25 14:10 smourier