You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure if this is a bug or missing feature. Building on the sample in #494, if I make a test method that runs asynchronously, I cannot get the return value back to javascript. On the javascript side, the promise seems to get resolved, but the return value is null.
Versions
SDK: 0.9.628-prerelease
Runtime: Dev (87.0.658.0)
Framework: WPF
Visual Studio: 2019 version 16.7.4
OS: Win10 1803 Build 17134.1726
HostObject code:
using System.Diagnostics;using System.Runtime.InteropServices;using System.Threading.Tasks;namespaceWebView2WpfBrowser{[ClassInterface(ClassInterfaceType.AutoDual)][ComVisible(true)]publicclassBrowserHostObject{publicstringTest(stringsomething){stringmessage=$"***Test({something}) Called***";
Debug.Print(message);return"Synchronous test succeeded";}publicasyncTask<string>TestAsync(stringsomething){stringmessage=$"***TestAsync({something}) Called***";
Debug.Print(message);// Simulate an async call to a remote machinereturnawait Task.Run(()=>"Asynchronous test succeeded");}}}
Test html page:
<!DOCTYPE html><html><head><title>Test Page</title><script>"use strict";asyncfunctionSendTestMessageAsync(){try{letmessage=document.getElementById("input-TestMessage").value;letexternal=awaitwindow.chrome.webview.hostObjects.external;letresult=external.TestAsync(message);result.then((res)=>{document.getElementById("result-TestMessage").value=res;});// This does not work either//let result = await external.TestAsync(message);//document.getElementById("result-TestMessage").value = result;}catch(e){document.getElementById("result-TestMessage").value=`Error: ${e.toString()}`;}}asyncfunctionSendTestMessage(){try{letmessage=document.getElementById("input-TestMessage").value;letexternal=awaitwindow.chrome.webview.hostObjects.external;letresult=awaitexternal.Test(message);document.getElementById("result-TestMessage").value=result;}catch(e){document.getElementById("result-TestMessage").value=`Error: ${e.toString()}`;}}</script></head><body><h1>Test Page</h1><h3>TestMessage</h3><inputtype="text" id="input-TestMessage" value="Test message from script on page" />
<buttononclick="SendTestMessageAsync()">SendAsync</button><buttononclick="SendTestMessage()">Send</button><br><textareaid="result-TestMessage" rows="4" readonly></textarea></body></html>
When you click the Send button, it works correctly because the .net method runs synchronously.
When you click the SendAsync button, a null result is returned. I verified I can call await BrowserHostObject.TestAsync() from .net code and it correctly returns the string.
Is there some way to get this async method returning a value all the way to javascript?
The text was updated successfully, but these errors were encountered:
We don't currently support async host object functions. #75 is tracking this work, and we have an item on our backlog that I've added your feedback to. Thanks!
Not sure if this is a bug or missing feature. Building on the sample in #494, if I make a test method that runs asynchronously, I cannot get the return value back to javascript. On the javascript side, the promise seems to get resolved, but the return value is null.
Versions
SDK: 0.9.628-prerelease
Runtime: Dev (87.0.658.0)
Framework: WPF
Visual Studio: 2019 version 16.7.4
OS: Win10 1803 Build 17134.1726
HostObject code:
Test html page:
When you click the
Send
button, it works correctly because the .net method runs synchronously.When you click the
SendAsync
button, a null result is returned. I verified I can callawait BrowserHostObject.TestAsync()
from .net code and it correctly returns the string.Is there some way to get this async method returning a value all the way to javascript?
The text was updated successfully, but these errors were encountered: