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 @@
+
+
+
+
+
+
+
+