MicrosoftEdge / MicrosoftEdge/WebView2Feedback

[Problem/Bug]: Inconsistent behavior for HostObject method with optional parameter called with upper/lower case first letter

Open
#4,510 8 comments 1 reaction 1 assignee View on GitHub

@LiangTheDev is already working on this.

Since Apr 26, 2024.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

What happened?

The bug was found in more complex project, but I reproduced it in the most simple way possible in separate clear WPF project. I added only WebView2 there and added HostObject by calling AddHostObjectToScript with code below:

...
        Loaded="Window_Loaded">
    <Grid>
        <wv2:WebView2 x:Name="webView2" Source="https://google.com/" />
    </Grid>
</Window>

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
    await webView2.EnsureCoreWebView2Async();
    webView2.CoreWebView2.AddHostObjectToScript("bridge", new BridgeHostObject());
}

To explore all basic possibilities the BridgeHostObject has following two methods:

[ComVisible(true)]
public class BridgeHostObject
{
    public string? TestMethodA(string? str)
    {
        return str;
    }

    public string? TestMethodB(string? str = null)
    {
        return str;
    }
}

I found out about the bug as we are moving an application from CefSharp to WebView2 and CefSharp supported calling HostObject methods with any first letter from JavaScript side so it was causing errors after switching to WebView2. However thanks to that I also found about following weird inconsistent behavior.

When calling methods with JavaScript using upper case first letter the behavior is as expected.

WebView2Issue-Screenshot01

When calling methods with JavaScript using lower case first letter the behavior is following where in case of TestMethodA the break point on the side of WPF application is not activated, but in case of TestMethodB it is and its parameter is always null even when value 'abcd' was entered from the side of the JavaScript.

WebView2Issue-Screenshot02

WebView2Issue-Screenshot03

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

123.0.2420.47

SDK Version

1.0.2420.47

Framework

WPF

Operating System

Windows 11

OS Version

Edition Windows 11 Enterprise
Version 22H2
Installed on ‎11. ‎4. ‎2024
OS build 22621.3447
Experience Windows Feature Experience Pack 1000.22688.1000.0

Repro steps
  1. Create simple application with the code in the description above
  2. Put breakpoints in both TestMethodA and TestMethodB
  3. Start the application
  4. Right click the WebView2 element and go to Inspect->Console
  5. Run await chrome.webview.hostObjects.bridge.testMethodA('abcd')
  6. No breakpoint is activated as expected
  7. Run await chrome.webview.hostObjects.bridge.testMethodB('abcd')

Expected behavior: No breakpoint activated as well

Actual behavior: Breakpoint for TestMethodB activated and the parameter str having value null instead of "abcd" and as the code was successfully executed and null was returned at least seeing that in Console would be expected, but not even that happened as "Unable to call method on non-function" error message was shown instead.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

Don't know

Last working version (if regression)

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.