12

I took the latest version of Microsoft.Web.WebView2 (0.9.515-prerelease) and added to a test C# WinForms application. Am using VS 2019, .NET framework is 4.7.2. Placed the WebView2 control on a form, compiled and ran. The application crashed on Load, at the below point in Form1.designer.cs.

    // webView21
    // 
    this.webView21.Location = new System.Drawing.Point(153, 66);
    this.webView21.Name = "webView21";
    this.webView21.Size = new System.Drawing.Size(492, 253);
    this.webView21.Source = new System.Uri("about:blank", System.UriKind.Absolute);
    this.webView21.TabIndex = 0;
    this.webView21.Text = "webView21";
    this.webView21.ZoomFactor = 1D;
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(800, 450);
    this.Controls.Add(this.webView21); //CRASHED HERE WITH BELOW EXCEPTION -

When I run this in Release mode, get the following exception trace - System.NullReferenceException: Object reference not set to an instance of an object.

   at Microsoft.Web.WebView2.WinForms.WebView2.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

If I take the latest stable version of WebView2 (v 0.9.488), it throws a compilation error as it does not reflect Microsoft.Web

Please let me know how to fix this error. Appreciate your help very much. Came across this question 8 months ago but am hoping Microsoft has gone beyond that now. How can I use the Microsoft Edge WebView2 control in C# windows application

The Sample solution Microsoft created does have C# version as well for WinForms. https://github.com/MicrosoftEdge/WebView2Samples

Tried this sample given by Microsoft as well. It too crashes. https://learn.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/winforms

Krishnan V S
  • 1,124
  • 1
  • 13
  • 31
  • Are you having right version of Microsoft Edge installed for WebView2? e.g WebView2 0.9.515-prerelease requires minimum of Microsoft Edge 84.0.515.0 installed? https://learn.microsoft.com/en-us/microsoft-edge/webview2/releasenotes – Erkki Nokso-Koivisto May 19 '20 at 15:03
  • The sample app says "Ensure a complete URL is entered in the address bar. An ArgumentException is thrown if the URL does not start with http:// or https://", and your app is trying to load "about:blank". Maybe that is the problem? – GreatBigGiantBrain May 19 '20 at 21:06
  • It does not even get to the load event. The exception is in the designer code itself, while the designer code adds the control to form. – Krishnan V S May 20 '20 at 19:17
  • Not sure if I am missing something very basic - I checked the version of MS Edge in laptop, it says this "Microsoft Edge is up to date. Version 81.0.416.77 (Official build) (64-bit)". But why is 84.0.* stated as the min version in that weblink ? – Krishnan V S May 20 '20 at 19:21

9 Answers9

6

you need to make sure 1) the Edge version is right. try download Canary version Edge from Url https://c2rsetup.officeapps.live.com/c2r/downloadEdge.aspx?ProductreleaseID=Edge&platform=Default&version=Edge&source=EdgeInsiderPage&Channel=Canary&language=en

and 2) set x86 as the target platform will fix the problem.

3

I faced the same issue but I was able to solve it. It crashes any time the traget CPU is "Any CPU" but if you change it to either "x86" or "x64", it runs successfully in my case. Seems it is not able to check and work with unspecified target CPU architecture.

I hope this saves someone lots of headaches

OPMat
  • 463
  • 4
  • 14
1

Unfortunately, this is still an issue in 0.9.515-prerelease. There's a bug opened against this and I added some more details about my repro:

https://github.com/MicrosoftEdge/WebViewFeedback/issues/228#issuecomment-687873636

Edit: The x86 suggestion does not apply for you if you install Edge for x64 bits. You can build it for x64 and it will work. More details in my response in the issue: https://github.com/MicrosoftEdge/WebViewFeedback/issues/228#issuecomment-687900181

1

Following the set platform guidance...

If you are using Canary Microsoft.Web.WebView2.0.9.628-prerelease. There is a known issue, the current solution is to downgrade to Microsoft.Web.WebView2.0.9.579-prerelease.

https://github.com/MicrosoftEdge/WebView2Feedback/issues/431

This worked for me.

Scooter
  • 364
  • 3
  • 8
0

from the path of my Edge, "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", we may conclude the current Edge is 32bit program? wat about give a try setting platform target to x86. some guru said, with x86 set, it will work!

from the MS sample project WebView2WindowsFormsBrowser.csproj, the Microsoft.Web.WebView2 version should be "0.9.515-prerelease".

0

As stated by others, the minimum Microsoft Edge version is 84.0.515.0 when using WebView2 0.9.515-prerelease. You can download the required Microsoft Edge version from the Microsoft Edge Insider Channel. See the link below.

Here are some useful links when using WebView2:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
0

The WebView2 component may generate an exception and crash the application during loading if it does not have permission to create the data folder needed to store user settings, etc.

If the application loads correctly when launched from Visual Studio or from other unrestricted locations, but fails to load when installed into Program Files, then this is a possible issue.

By default, the data folder is created in the same folder as the application exe, which can fail in Program Files. The data folders needs to be set to an alternative location.

This link provides some info on the use of data folders: Managing the User Data Folder

This issue provides some code for setting the data folder for WebView2 to a valid location: WebView2 C# Winforms application doesn't work when installed in Program Files folder #297

aoc
  • 1
  • 1
0

This worked for me:

 var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\OurSoftware";
var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
Skyfish
  • 119
  • 2
  • 4
0

I encounter this problem today. My app works flawlessly until today. Turned out Edge Canary has automatically updated, and my WebView2 library (ver. 1.0.11xx) in app become deprecated. I use Nuget Package Manager. So I open Nuget Package Manager menu, Package Manager Console. Type in

Update-Package

After update, my WebView2 library updated to 1.0.13xx version. My app continues to work again.

Wild Teddy
  • 334
  • 8
  • 7