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

"WebView2 was already initialized with a different CoreWebView2Environment" #1782

Closed
Ssiws opened this issue Sep 30, 2021 · 7 comments
Closed
Labels
bug Something isn't working

Comments

@Ssiws
Copy link

Ssiws commented Sep 30, 2021

Description
Since 1.0.992.28, I'm unable to use EnsureCoreWebView2Async(env), webview throws an exception when I try to set the Source property for the first time.

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.'

Version
SDK: 1.0.992.28
Runtime: 96.0.1031.0
Framework: WPF
OS: Win10 2004

Repro Steps

var webView = new Microsoft.Web.WebView2.Wpf.WebView2();
var env = CoreWebView2Environment.CreateAsync(null, null, new CoreWebView2EnvironmentOptions
{
     AllowSingleSignOnUsingOSPrimaryAccount = true
}).Result;
 webView.Loaded += async (sender, e) =>
    {
       await webView.EnsureCoreWebView2Async(env);
       webView.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
       webView.CoreWebView2.NavigationStarting += webView_NavigationStarting;
       webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
       webView.Source = new Uri("https://www.microsoft.com/");
};
   mainGrid.Children.Clear();
   mainGrid.Children.Add(webView);

Screenshots

Additional context

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

me too. anyone has a solution?it troubles me a lot.

@rozeboosje
Copy link

rozeboosje commented Sep 30, 2021

Yes, me too. Please fix.

Also: when I try to use EnsureCoreWebView2Async without an environment it seems to be working up to a point. It stops working when I then try to use it in an Outlook Addin. From the little information I can glean it seems like it's already set up with its own environment then (I am guessing by Outlook itself?), but when I then try to use a WebView2 in my own Add-in on, say, a WPF form it fails because it doesn't have permissions to write to the Outlook WebView folder (in my case "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE.WebView2"). I can get it to work then by giving full permissions to "everyone" on that folder, but I can't burden our support staff to do that for every user they need to install our software for, and since we also provide software for larger organisations I know that their IT support teams will not accept that "solution" either.

The problem goes away when I revert back to nuGet package 1.0.961.33

@tiagoabreu
Copy link

tiagoabreu commented Oct 4, 2021

I have the same issue. I want to add that I am absolutely sure passing the same environment in both calls to EnsureCoreWebView2Async. I understand the comparison is made by reference, and mine didn't change. Looking at the WebView2 code, I could not figure out where is there reference for Environment set.
I would suggest a better equality operation, to make sure the settings are indeed the same.

But according to the release notes:
"Removed silent failures by using EnsureCoreWebView2Async, which throws an ArgumentException when called multiple times with incompatible parameters." should only happen after version 1.0.1010-prerelease.
https://docs.microsoft.com/en-us/microsoft-edge/webview2/release-notes#experimental-features-1

Why is it happening on the latest stable?

@champnic
Copy link
Member

champnic commented Oct 5, 2021

This is a duplicate to #1782. The easiest workaround is to use WebView2.CoreWebView2.Navigate instead of setting the source property. Or use 1.0.961.33 SDK package.

@tiagoabreu The confusion here stems from how our branches work internally. This change was committed to our version 982, but our first prerelease package after that was 1010. However, we forked for stable release branch around version 992, which also included the change (because > 982). I'll open this as a bug to see if we can improve process or add documentation around our versioning to explain this better.

@champnic
Copy link
Member

@GJStarCode If that is throwing a null reference, that means the WebView2 has not finished initializing. Are you awaiting the call to EnsureCoreWebView2Async? Or you can add a listener for CoreWebView2Initialized, verify that it succeeded using the args, and then call Navigate.

@champnic
Copy link
Member

@GJStarCode Can you take this info and create a new GitHub issue? This looks different than the original issue here. It's also interesting that it only repros when run from the exe, and not from Visual Studio.

@GJStarCode
Copy link

Hi Nic I've created a new issue, i'll clean this one
The number is #2892

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

6 participants