From 30ad162c698ef14c2e786fa7fbadf961e28d4d02 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 17 Oct 2026 10:58:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=94=A4=E8=B5=B7=EF=BC=9A?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E8=87=AA=E5=8A=A8=E6=8C=87=E5=90=91=E5=BD=93?= =?UTF-8?q?=E5=89=8D=20exe=20+=20=E5=8D=95=E5=AE=9E=E4=BE=8B=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E6=8F=90=E5=89=8D=20+=20=E8=87=AA=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E5=8F=91=E5=B8=83=EF=BC=88=E4=B8=8D=E5=86=8D=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=20DOTNET=5FROOT=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:网页点「启动本机软件」/双击 exe 弹「You must install .NET Desktop Runtime」, 网页表现为唤起没反应、查询失败。 根因有两个: 1. 协议注册表指向的是 bin\Debug 的 framework-dependent exe,它靠 DOTNET_ROOT 找运行时; 而浏览器/资源管理器/协议拉起的进程拿不到我们临时设的环境变量 → 直接弹安装提示。 更糟的是协议只在主窗口手动点按钮才注册,换目录/换构建后注册表一直是旧路径。 2. 单实例的命名管道监听是在「登录完成、主窗口出现」之后才 StartListening, 用户卡在登录窗期间被协议唤起 → 参数无人接收(第二个实例连不上管道就直接退出了)。 修复: - App.EnsureProtocolRegistered():每次启动把 HKCU 的 command 校正到 Environment.ProcessPath, 与当前 exe 一致时跳过(幂等);不一致时重注册并记日志(含原路径,方便排查)。 - _single.StartListening() 提前到启动服务之后、登录之前。 - 登录期间收到唤起:存 pending code 并把登录窗叫到前面,登录完成后自动查询(原有逻辑保留)。 - 抽出 BringLoginWindowToFront(),OnLoginRequested / 第二次双击(不带 URL)也走它—— 以前登录期间再双击 exe 是彻底没反应的。 - MainWindow 的协议状态改为显示「是否指向当前 exe」,指向旧路径时提示重启会自动校正。 分发形态改为自包含: - desktop/nuget.config 指向华为云 NuGet 镜像(本机访问不了 api.nuget.org), 自包含发布要下载 Microsoft.*.App.Runtime.win-x64; - dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -o publish → 单文件约 154 MB,双击/协议唤起都能直接跑。 - publish/ 已在 .gitignore 里。 实测(全程清掉 DOTNET_ROOT,模拟用户环境): - 冷启动:yuncang://open?code=COLD123 → 进程起来 → 恢复登录 → 自动查询 COLD123; - 已运行时:yuncang://open?code=TEST999 → 管道转发到原实例 → 主窗口前置并查询; - 注册表自动从 bin\Debug 校正到 publish\...exe(日志有记录); - 网页 /local?code=ABC123 → 已登录 → 正常展示查询结果(真实桌面端,非 mock)。 --- desktop/App.xaml.cs | 71 ++++++++++++++++++++++++++++---------- desktop/MainWindow.xaml.cs | 14 ++++++-- desktop/README.md | 30 ++++++++++++---- desktop/nuget.config | 12 +++++++ 4 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 desktop/nuget.config diff --git a/desktop/App.xaml.cs b/desktop/App.xaml.cs index 2af583b..2f05da7 100644 --- a/desktop/App.xaml.cs +++ b/desktop/App.xaml.cs @@ -58,6 +58,13 @@ public partial class App : Application _server.LoginRequested += OnLoginRequested; StartLocalServer(); + // 启动协议永远指向「当前这个 exe」:换目录/从 Debug 换成发布版/重新安装后, + // 旧注册表项会指向一个不存在的或依赖开发环境的 exe(网页“启动本机软件”会直接报错)。 + EnsureProtocolRegistered(); + + // 单实例监听要**在登录之前**起来:用户卡在登录窗时被协议唤起,参数也要能进来。 + _single.StartListening(); + // ③ 托盘 var version = GetType().Assembly.GetName().Version?.ToString() ?? "1.0.0"; _tray = new TrayIconService(_server.BaseUrl, version); @@ -76,7 +83,32 @@ public partial class App : Application } ShowMainWindow(); - _single.StartListening(); + } + + /// + /// 把 yuncang://open?code=xxx 的注册表项校正到当前 exe。 + /// 已经指向当前 exe 时什么都不做(幂等,每次启动都能安全调用)。 + /// + private void EnsureProtocolRegistered() + { + try + { + var exe = Environment.ProcessPath; + if (string.IsNullOrEmpty(exe)) return; + + var want = $"\"{exe}\" \"%1\""; + var current = ProtocolHelper.RegisteredCommand(); + if (string.Equals(current, want, StringComparison.OrdinalIgnoreCase)) return; + + ProtocolHelper.Register(); + AppLog.Write(current is null + ? $"已注册启动协议 {ProtocolHelper.Scheme}:// → {exe}" + : $"启动协议已改指向当前程序:{exe}(原来是 {current})"); + } + catch (Exception ex) + { + AppLog.Write($"注册启动协议失败:{ex.Message}"); + } } // ---------------- 登录 ---------------- @@ -155,23 +187,23 @@ public partial class App : Application return; } - // 没登录:登录窗是模态的,一直在,只要把它叫到最前面 - if (_loginWindow is not null) - { - if (_loginWindow.WindowState == WindowState.Minimized) _loginWindow.WindowState = WindowState.Normal; - - _loginWindow.Activate(); - _loginWindow.Topmost = true; - _loginWindow.Topmost = false; - _loginWindow.Focus(); - return; - } - - // 兜底(正常不会走到):直接走一次登录流程 - if (ShowLogin()) ShowMainWindow(); + BringLoginWindowToFront(); }); } + /// 把登录窗弹到最前(没登录时网页/协议/第二次双击都走这里) + private void BringLoginWindowToFront() + { + if (_loginWindow is null) return; + + if (_loginWindow.WindowState == WindowState.Minimized) _loginWindow.WindowState = WindowState.Normal; + + _loginWindow.Activate(); + _loginWindow.Topmost = true; + _loginWindow.Topmost = false; + _loginWindow.Focus(); + } + /// 退出登录:清令牌 → 关主窗口 → 重新登录(取消则退出程序) private void OnLogoutRequested() { @@ -294,8 +326,9 @@ public partial class App : Application if (_main is null) { - // 还在登录界面:等登录完再查 - _pendingLaunchUrl = url; + // 还在登录界面:先把登录窗叫到前面,参数留着登录完再查 + if (url is not null) _pendingLaunchUrl = url; + BringLoginWindowToFront(); return; } @@ -304,7 +337,9 @@ public partial class App : Application return; } - ShowAppWindow(); + // 第二次双击 exe(没带 URL):有主窗口就显示,还在登录就把登录窗叫到前面 + if (_main is null) BringLoginWindowToFront(); + else ShowAppWindow(); }); } diff --git a/desktop/MainWindow.xaml.cs b/desktop/MainWindow.xaml.cs index 8a7b117..0b0ef70 100644 --- a/desktop/MainWindow.xaml.cs +++ b/desktop/MainWindow.xaml.cs @@ -145,11 +145,19 @@ public partial class MainWindow : Window private void UpdateProtocolState() { var registered = ProtocolHelper.IsRegistered(); + var command = ProtocolHelper.RegisteredCommand(); + var exe = Environment.ProcessPath; + + // 注册表指向的必须就是当前这个 exe(否则网页叫醒拿到的是旧路径/开发目录里的 exe) + var pointsToMe = command is not null && exe is not null + && command.IndexOf(exe, StringComparison.OrdinalIgnoreCase) >= 0; ProtocolButton.Content = registered ? "取消启动协议" : "注册启动协议"; - ProtocolText.Text = registered - ? $"已注册 {ProtocolHelper.Scheme}://(网页可叫醒本软件)" - : $"未注册 {ProtocolHelper.Scheme}://(网页无法叫醒本软件)"; + ProtocolText.Text = !registered + ? $"未注册 {ProtocolHelper.Scheme}://(网页无法叫醒本软件)" + : pointsToMe + ? $"已注册 {ProtocolHelper.Scheme}://(网页可叫醒本软件)" + : $"已注册,但指向旧路径(重启本软件会自动校正)"; } // ---------------- 查询 ---------------- diff --git a/desktop/README.md b/desktop/README.md index 9357574..1e78809 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -94,13 +94,25 @@ dotnet build # 编译(Debug = 开发环境,连 dotnet build -c Release # 编译(Release = 生产环境,默认地址见上一节) dotnet run # 直接跑(调试) -# 发布给用户装(framework-dependent,体积小、需要装 Desktop Runtime) -dotnet publish -c Release -r win-x64 -p:ServerBaseUrl=https://trace.example.com --self-contained false -o publish - -# 或者自带运行时(体积大,但用户什么都不用装) -dotnet publish -c Release -r win-x64 -p:ServerBaseUrl=https://trace.example.com --self-contained true -p:PublishSingleFile=true -o publish +# ★ 发给用户、以及要用「网页叫醒」的话,必须用自包含发布 +dotnet publish -c Release -r win-x64 --self-contained true ` + -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true ` + -p:ServerBaseUrl=https://正式域名 -o publish ``` +**为什么要自包含**:`dotnet build` 出来的 exe 是 *framework-dependent* 的, +它自己会去找 `C:\Program Files\dotnet` 或 `DOTNET_ROOT`。从**浏览器/资源管理器/协议**拉起的进程 +拿不到我们临时设的环境变量,于是弹「You must install .NET Desktop Runtime」—— +网页上表现就是「唤起没反应 / 弹安装提示」。 + +自包含单文件(约 154 MB)把运行时打进去了,双击和协议唤起都能直接跑,用户什么都不用装。 + +> `desktop/nuget.config` 指向华为云 NuGet 镜像(本机访问不了 api.nuget.org)—— +> 自包含发布要下载 `Microsoft.*.App.Runtime.win-x64` 运行时包,没有镜像会 restore 失败。 + +开发阶段想让 `bin\Debug` 的 exe 也能直接双击,两个办法:装官方 SDK/Desktop Runtime 到标准位置, +或者每次用发布产物验证唤起。 + ## 本机接口(前端 `/local` 页用的就是这三个) 服务固定监听 `http://localhost:17300`(端口写在前端页面与插件里)。 @@ -145,7 +157,10 @@ HKEY_CURRENT_USER\Software\Classes\yuncang (默认) = "C:\...\YuncangTrace.LocalService.exe" "%1" ``` -- 界面上点「**注册启动协议**」即可(指向当前 exe);卸载时点「取消启动协议」或调用 `ProtocolHelper.Unregister()`; +- **每次启动都会自动把注册项校正到「当前这个 exe」**(幂等)。这一点很关键: + 换目录安装、从 `bin\Debug` 换成发布版、重新装一遍之后,注册表里残留的旧路径会让网页 + 唤起一个不存在 / 缺运行时的 exe(典型现象就是弹「You must install .NET」或干脆没反应)。 + 界面上仍可手动「取消启动协议」; - 手工注册/排查: ```powershell @@ -170,6 +185,8 @@ reg query "HKCU\Software\Classes\yuncang\shell\open\command" - **已登录** → 直接调 `/api/query/{code}` 展示(页面顶部会显示「本机软件已登录:xxx」)。 3. 本软件被协议拉起时,会拿到 `yuncang://open?code=xxx` 参数:已经开着就转给原窗口(单实例), 没开就启动 → 先登录 → 登录完自动查询这个码(登录前收到的 code 会先存着)。 + 单实例的命名管道监听**在登录之前就起来了**,所以用户卡在登录窗时被网页唤起也不会丢参数 + (只会把登录窗叫到前面,登录完自动查)。 ## ★ 接真实数据 @@ -205,4 +222,5 @@ public QueryResult Query(string? code) | 报端口被占用(错误 32) | 已经开了一个实例(单实例会把参数转过去);检查任务管理器里的残留进程 | | 网页提示服务不可用,但软件在跑 | 先用浏览器直接打开 `http://localhost:17300/api/health`:打不开→防火墙/端口;打得开→多半是 CORS 头没回 | | 点网页「启动本机软件」没反应 | 协议没注册(界面看「启动协议」状态);或浏览器拦了自动跳转(必须用户点击) | +| 唤起时弹「You must install .NET Desktop Runtime」 | 唤起的是 `bin\Debug` 这类 framework-dependent 的 exe(拿不到 `DOTNET_ROOT`)。用自包含发布产物,并让它启动一次以校正注册表 | | 改了协议名 | 注册表重注册 + 前端 `VITE_APP_PROTOCOL` 同步 + 插件目标地址同步 | diff --git a/desktop/nuget.config b/desktop/nuget.config new file mode 100644 index 0000000..f0ba0c3 --- /dev/null +++ b/desktop/nuget.config @@ -0,0 +1,12 @@ + + + + + + + +