I created a new .net 8.0 winforms project in vs2022, it includes the latest webview2 nuget package.
myawesomeprogram.cs
namespace myawesomenamespace
{
internal static class myawesomeprogram
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new myawesomeform());
}
}
}
myawesomeform.designer.cs
namespace myawesomenamespace
{
partial class myawesomeform
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
myawesomewebview = new Microsoft.Web.WebView2.WinForms.WebView2();
((System.ComponentModel.ISupportInitialize)myawesomewebview).BeginInit();
SuspendLayout();
myawesomewebview.AllowExternalDrop = true;
myawesomewebview.CreationProperties = null;
myawesomewebview.DefaultBackgroundColor = Color.White;
myawesomewebview.Dock = DockStyle.Fill;
myawesomewebview.Location = new Point(0, 0);
myawesomewebview.Name = "myawesomewebview";
myawesomewebview.Size = new Size(800, 450);
myawesomewebview.TabIndex = 4;
myawesomewebview.ZoomFactor = 1D;
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(myawesomewebview);
Name = "myawesomeform";
Text = "myawesomeform";
WindowState = FormWindowState.Maximized;
Load += myawesomeform_Load;
((System.ComponentModel.ISupportInitialize)myawesomewebview).EndInit();
ResumeLayout(false);
}
private Microsoft.Web.WebView2.WinForms.WebView2 myawesomewebview;
}
}
myawesomeform.cs
using Microsoft.Web.WebView2.Core;
namespace myawesomenamespace
{
public partial class myawesomeform : Form
{
private Random? myawesomerandom;
public myawesomeform()
{
InitializeComponent();
}
private async void myawesomeform_Load(object sender, EventArgs e)
{
CoreWebView2Environment? myawesomeenvironment = await CoreWebView2Environment.CreateAsync();
await myawesomewebview.EnsureCoreWebView2Async(myawesomeenvironment);
myawesomerandom = new Random();
await Task.Delay(myawesomerandom.Next(1000, 2000));
myawesomewebview.Source = new Uri("http://lixxx.co.uk");
await Task.Delay(myawesomerandom.Next(1000, 2000));
myawesomewebview.Source = new Uri("http://lmfgtfy.co.uk");
}
}
}
It runs on my development machine perfectly well.
I configured a publish profile with a release configuration, a .net 8.0 windows target framework, a win x64 target runtime, a self contained deployment mode using the single file and ready to run compilation options.
The published .exe file, in its published directory, runs correctly.
When I cut and paste the entire published directory onto a 64 bit AMD server, running windows server 2022 and run the .exe, the application loads and immediately displays an unhandled error exception dialogue with the exception text ‘System.IO.FileNotFoundException: The system cannot find the file specified. (0x80070002)’.
The application had a requirement to write to a text file, but I have since deleted it from the latest builds.
The server has the .net 8.0 hosting bundle and the .net 8.0 x64 runtime installed.