Microsoft Edge WebView2 - Sample crashes on Load

Viewed 9203

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://docs.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/winforms

8 Answers

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

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

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:

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

This worked for me:

 var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\OurSoftware";
var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
Related