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

WPF Sample + AddHostObjectToScript: async methods with return value do not work #532

Closed
lostobject opened this issue Oct 13, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@lostobject
Copy link

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;

namespace WebView2WpfBrowser
{
	[ClassInterface(ClassInterfaceType.AutoDual)]
	[ComVisible(true)]
	public class BrowserHostObject
	{

		public string Test(string something)
		{
			string message = $"***Test({something}) Called***";
			Debug.Print(message);
			return "Synchronous test succeeded";
		}

		public async Task<string> TestAsync(string something)
		{
			string message = $"***TestAsync({something}) Called***";
			Debug.Print(message);
			// Simulate an async call to a remote machine
			return await Task.Run(() => "Asynchronous test succeeded");
		}
	}
}

Test html page:

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <script>
        "use strict";
        async function SendTestMessageAsync() {
            try {
                let message = document.getElementById("input-TestMessage").value;
                let external = await window.chrome.webview.hostObjects.external;
                
                let result = 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()}`;
            }
        }

        async function SendTestMessage() {
          try {
            let message = document.getElementById("input-TestMessage").value;
            let external = await window.chrome.webview.hostObjects.external;
            let result = await external.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>
<input type="text" id="input-TestMessage" value="Test message from script on page" />
<button onclick="SendTestMessageAsync()">SendAsync</button>
<button onclick="SendTestMessage()">Send</button>
<br>
  <textarea id="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?

@lostobject lostobject added the bug Something isn't working label Oct 13, 2020
@champnic
Copy link
Member

champnic commented Oct 14, 2020

Sorry I misread the issue. Re-reading and I'll have a better response.

@champnic
Copy link
Member

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!

@lostobject
Copy link
Author

Ok thanks Nic. I have added more context to #75 in case that helps.

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

2 participants