Welcome Guest Search | Active Topics | Sign In | Register

EO WebBrowser exception: Object can not be passed to JavaScript code Options
Nobody
Posted: Tuesday, December 14, 2021 10:17:19 AM
Rank: Newbie
Groups: Member

Joined: 12/14/2021
Posts: 3
Hi there, I've encountered a problem when performing JS command (loading a content in WebView).

Here is an exception:
-- Message: --
Object 'System.Threading.Tasks.Task`1[System.Object]' of type 'System.Threading.Tasks.Task`1[System.Object]' can not be passed to JavaScript code.

-- Source: --
EO.WebBrowser

-- Stack trace: --
at EO.WebBrowser.JSObject.a(Object A_0, l1 A_1)
at EO.WebBrowser.WebView.v(co A_0, ap9 A_1)
at EO.WebBrowser.WebView.a(co A_0, ap9 A_1)
at EO.WebBrowser.WebView.b(co A_0, ap9 A_1)

and my code:
Code: C#
public async Task<T> CallTopScreenWithResult<T>(string header, object obj)
        {
            if (WebControlTopScreen?.WebView == null || _lastWebState != WebAppState.Online)
                throw new InvalidOperationException("Can not call Top screen at this moment");

            var result = await WebControlTopScreen.WebView.PerformJsCommandWithResultAsync<T>("listenTopScreen", new object[] { header, obj });
            if (result is JSUndefined)
                result = default(T);

            return result;
        }

public static async Task<T> PerformJsCommandWithResultAsync<T>(this WebView webView, string methodName, object[] methodArguments)
        {
            try
            {
                if (webView.CreatedByThisThread() == false && webView.CanEvalScript)
                    return (T)webView.EvalScript(GetJavascriptFunctionString(methodName, methodArguments), true);

                var taskCompletionSource = new TaskCompletionSource<object>();
                var call = webView.QueueScriptCall(GetJavascriptFunctionString(methodName, methodArguments));
                call.OnDone(() => taskCompletionSource.SetResult(call.Result));

                return (T)await taskCompletionSource.Task;
            }
            catch (Exception ex)
            {
                LogJavascriptExceptionMessage(ex, methodName, methodArguments);
                throw;
            }
        }


This output is from my WPF app, EO lib. version 19.2.91.
The most interesting is that I use this same method in WinForms, same EO version and it works very well.

I've also tried the latest version but it hasn't helped unfortunately.
Let me know if more info needed, thanks!
eo_support
Posted: Tuesday, December 14, 2021 10:25:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

Can you isolate the problem into a small test project and then send the test project to us? See here for more details:

https://www.essentialobjects.com/forum/test_project.aspx

Once we receive the test project, we will debug it here and see what we can find.

Thanks!
Nobody
Posted: Wednesday, December 15, 2021 5:45:27 AM
Rank: Newbie
Groups: Member

Joined: 12/14/2021
Posts: 3
I was talking to my calleagues about this option and was told that it would be difficult to reproduce the problem in a separate project according to the all dependencies etc.
The thing is that our WPF app consists of two windows which have each browser presenting some web content. When you
1. trigger an action in 1st window (BottomScreen - CallTopScreenWithResult)
2. it passes the information to the 2nd window (TopScreen - CallBottomScreenWithResult) and this one loads up the new content
3. then returns some more info (id detail which we wouldn't know whithout this step and need it for performing further actions in BottomScreen) about presenting content back to 1st window

1. and 2. step is performed with no issues until 3. step where the EO exception turns up with mentioned a message above.
All this is made using JS functions (RegisterJSExtensionFunction).

Maybe I will give it a try but it may behave completely differently from how it does now.
Nobody
Posted: Wednesday, December 15, 2021 7:03:52 AM
Rank: Newbie
Groups: Member

Joined: 12/14/2021
Posts: 3
I've just finally resolved this!

Solution is replace this:
WebControlTopScreen.WebView.RegisterJSExtensionFunction("CallBottomScreen", (sender, args) =>
args.ReturnValue = SportboxView.Singleton.CallBottomScreenWithResult<object>(args.Arguments[0] as string, args.Arguments[1]));

with this:
WebControlTopScreen.WebView.RegisterJSExtensionFunction("CallBottomScreen", async (sender, args) =>
args.ReturnValue = await SportboxView.Singleton.CallBottomScreenWithResult<object>(args.Arguments[0] as string, args.Arguments[1]));

so someone who has implemented this before has forgotten to add async.
Thanks anyway :)
eo_support
Posted: Wednesday, December 15, 2021 9:17:49 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Great. Thanks for sharing the solution!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.