You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft.Web.WebView2.WinForms.WebView2多实例视图加载失败问题排查

问题描述

开发了一个通过Windows窗体加载.cshtml视图的简单桌面应用,编译发布到远程机器D盘指定文件夹后,单用户运行一切正常,但已有实例运行时,其他用户启动应用会显示空白窗体。

已给代码添加日志和try{} catch{}块,但未获取有效信息。提供视图的内网服务在浏览器中始终能正常返回结果。

更新1:CoreWebView2InitializationCompleted事件捕获到异常 -> 请求的资源正在使用中。(0x800700AA)

附带代码

#region Windows Form Designer generated code

/// <summary>
///  Required method for Designer support - do not modify
///  the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
    this.webView = new Microsoft.Web.WebView2.WinForms.WebView2();
    ((System.ComponentModel.ISupportInitialize)(this.webView)).BeginInit();
    this.SuspendLayout();
    // 
    // webView
    // 
    this.webView.AllowExternalDrop = false;
    this.webView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.webView.CreationProperties = null;
    this.webView.DefaultBackgroundColor = System.Drawing.Color.White;
    this.webView.Location = new System.Drawing.Point(10, 10);
    this.webView.Name = "webView";
    this.webView.Size = new System.Drawing.Size(690, 125);
    this.webView.TabIndex = 0;
    this.webView.ZoomFactor = 1D;
    // 
    // MainForm
    // 
    this.AccessibleDescription = "MessageBoard Wrapper";
    this.AccessibleName = "MessageBoard Wrapper";
    this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(710, 145);
    this.Controls.Add(this.webView);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.MinimumSize = new System.Drawing.Size(250, 100);
    this.Name = "MainForm";
    this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    this.Text = "MessageBoard Wrapper";
    ((System.ComponentModel.ISupportInitialize)(this.webView)).EndInit();
    this.ResumeLayout(false);

}

#endregion

private HttpClient GetClient()
{
  var result = new HttpClient();
  return result;
}

private Microsoft.Web.WebView2.WinForms.WebView2 webView;

private string _baseUrl = "here I have my service url";

public MainForm()
{
    
    InitializeComponent();
    FormBorderStyle = FormBorderStyle.None;
    DoubleBuffered = true;
    SetStyle(ControlStyles.ResizeRedraw, true);
    var client = GetClient();
        try
        {
            var targetmUrlForCustomBusinessLogic = "url address here";
            var response = client.GetAsync(targetmUrlForCustomBusinessLogic).Result;

            if (response.IsSuccessStatusCode)
            {
                var t = Task.Run(() => response.Content.ReadAsStringAsync()).Result;

             //here I have some code too

            }

        }
        catch (Exception ex)
        {
            var message = ex.Message;
            //TODO: add logging here
          
        }
        finally
        {
            client.Dispose();
        }

    webView.Source = new Uri(_baseUrl);

    webView.NavigationCompleted += WebView_NavigationCompleted;

    ResizeEnd += (object sender, EventArgs e) => SaveFormSettings();
    Move += (object sender, EventArgs e) => SaveFormSettings();
    Shown += (object sender, EventArgs e) => LoadFormSettings();
}

空白窗体截图

空白窗体截图

可能原因与排查方向
  • WebView2用户数据目录冲突:WebView2默认使用共享数据目录(如发布目录下的EBWebView文件夹),多用户同时运行时,会因文件锁定或权限问题导致初始化失败,0x800700AA错误正是资源被占用的典型表现。
  • 发布目录权限不足:D盘应用文件夹未给所有用户分配读写权限,导致第二个用户无法创建或访问WebView2必要文件。
  • WebView2进程复用冲突:默认配置下WebView2可能复用进程,多用户场景下进程隔离不足引发资源竞争。
解决建议
  1. 为每个用户设置独立的WebView2数据目录:在WebView2初始化前,通过CoreWebView2CreationProperties指定唯一目录,比如结合用户名创建:
var userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YourAppName", Environment.UserName);
webView.CreationProperties = new CoreWebView2CreationProperties()
{
    UserDataFolder = userDataFolder
};

需在InitializeComponent()之前设置该属性,确保WebView2初始化时读取到配置。

  1. 调整发布目录权限:给D盘应用文件夹的Authenticated Users或Everyone组分配读写权限,避免文件访问被拒绝。

  2. 启用WebView2独立进程模式:通过代码配置CoreWebView2Environment强制使用独立进程,减少多用户间的资源竞争。

  3. 完善日志捕获:在CoreWebView2InitializationCompleted事件中记录完整异常信息,包括堆栈跟踪和内部异常:

webView.CoreWebView2InitializationCompleted += (s, e) =>
{
    if (!e.IsSuccess)
    {
        var errorLog = $"WebView2初始化失败: {e.InitializationException.Message}\n堆栈: {e.InitializationException.StackTrace}";
        File.AppendAllText("webview_error.log", errorLog);
    }
};

内容的提问来源于stack exchange,提问作者Eugene

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 01:10:19