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

EnsureCoreWebView2Async is internally called a 2nd time and throws the exception #1781

Closed
SchreinerK opened this issue Sep 30, 2021 · 9 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@SchreinerK
Copy link

SchreinerK commented Sep 30, 2021

EnsureCoreWebView2Async is internally called a 2nd time and throws the exception

System.ArgumentException: WebView2 was already initialized with a different CoreWebView2Environment. Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values.
   at Microsoft.Web.WebView2.Wpf.WebView2.EnsureCoreWebView2Async(CoreWebView2Environment environment)
   at Microsoft.Web.WebView2.Wpf.WebView2.<>c__DisplayClass52_0.<SourcePropertyChanged>b__0()
   at Microsoft.Web.WebView2.Wpf.WebView2.WhenInit_Source(Action action)
   at Microsoft.Web.WebView2.Wpf.WebView2.SourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Microsoft.Web.WebView2.Wpf.WebView2.set_Source(Uri value)

Steps to reproduce:

  • (A) handle CoreWebView2.NewWindowRequested, to initialize the WebView2 on a new tab
  • (B) set WebView2.Uri on the new tab to navigate to other page (user inputs a new URL)

I think maybe the problem is that EnsureCoreWebView2Async is called because the Uri is set the first time
BUT should not be called because environment already set in NewWindowRequested handler.

(A)

var environment = referrer?.WebView2.CoreWebView2.Environment;
var d = e.GetDeferral();
await WebView2.EnsureCoreWebView2Async(environment);
e.NewWindow = WebView2.CoreWebView2;
e.Handled = true;
d.Complete();

(B)

private void NavigateToUri(Uri uri) {
	if (WebView2.Source == null) InitCreationProperties(); 
	WebView2.Source = uri;
}

InitCreationProperties() is not called in this case because Source is not null

SDK: 1.0.992.28
Framework: WPF
OS: Win10

AB#36602860

@SchreinerK SchreinerK added the bug Something isn't working label Sep 30, 2021
@rozeboosje
Copy link

rozeboosje commented Sep 30, 2021

Following as this appears to be part of the bigger issue reported by several users in the last 24 hours and that affects me, too

@JooJooBee666
Copy link

JooJooBee666 commented Oct 1, 2021

Yeah, they really screwed the pooch on this 1.0.992.28 "stable" update. Seems EnsureCoreWebView2Async is just a broken mess for for everyone. See issues #1778 and #1782 as well. Hopefully they fix it soon. 👍

@rozeboosje
Copy link

Agreed. In fact I have now reverted to Build 1.0.902.49 as that is the only one that appears to work consistently for me across standalone WPF applications as well as our Outlook and Office Add-ins.

@champnic champnic added the tracked We are tracking this work internally. label Oct 5, 2021
@champnic
Copy link
Member

champnic commented Oct 5, 2021

Hey @SchreinerK - Thanks for the bug report! In general this new behavior is "By Design", in that we are trying to catch instances where devs were causing initialization to happen twice, and the second time was being silently ignored. For example, if the Source property was set (which triggers implicit initialization) and then the dev tried to call EnsureCoreWebView2Async with a specific environment setup, the environment was being silently ignored, causing unexpected behavior (missing browser args or settings, etc.).

However, you have found a case where we don't want this to happen, which is setting the Source property after EnsureCoreWebView2Async has completed initialization, only within the WPF control. This shouldn't cause a new implicit initialization because initialization has already completed! The Winforms control correctly allows the Source property to be set after EnsureCoreWebView2Async without an error.

The workarounds available to you are:

  1. Use the WebView2.CoreWebView2.Navigate function instead of setting the Source property.
  2. Use try/catch(ArgumentException) around the Source call and ignore the error. The navigation will still happen.
  3. Downgrade to a different SDK package.

I've opened this bug on our backlog and we'll get a fix out shortly. Thanks!

@champnic
Copy link
Member

This should now be fixed in SDK packages 1.0.1020.30 and 1.0.1056-prerelease. Thanks!

@rozeboosje
Copy link

Hi all.

I'm afraid this is still not working for me after upgrading to 1.0.1020.30

Can somebody please help as I am at my wit's end here. :(

@cosmin-teslovan
Copy link

I just upgraded to 1.0.1020.30 and the exception is no longer thrown for WPF.

@rozeboosje
Copy link

Yes ignore my comment I think I'm still struggling with #1778

@DanDev206
Copy link

I posted the solution here:
#2435 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

6 participants