给前端 `/local` 公共页与 Chrome 插件对接的本机软件(net8.0-windows + UseWPF):
- Services/LocalHttpServer.cs:HttpListener 监听 localhost:17311(同时注册 127.0.0.1 前缀)
- GET /api/health 探活(2xx 即认为已启动)
- GET /api/query/{code} 按运单号查信息,成功时字段平铺返回
- CORS 全量响应头 + Access-Control-Allow-Private-Network(公网 https 页面访问 localhost 的
PNA 预检),预检 OPTIONS 直接 204
- 根路径给一个说明页,方便浏览器里手工排查
- Services/OrderQueryService.cs:★ 接真实数据源的位置(现为演示数据)
- Services/ProtocolHelper.cs:HKCU\Software\Classes\yuncang 注册/反注册、解析
yuncang://open?code=xxx(免管理员)
- Services/SingleInstance.cs:互斥体 + 命名管道,被协议再次拉起时把参数转给已运行窗口,
不会去抢 17311 端口
- MainWindow:运行状态、服务地址(复制/浏览器打开)、注册启动协议、测试查询、运行日志
- App.xaml.cs:单实例判断 + 协议参数处理;启动即自动起服务
README 里写了编译/发布命令、接口契约、协议注册(含 reg 命令与打包工具做法)、
接真实数据的示例,以及端口权限(netsh http add urlalloc)/端口占用等常见问题。
⚠️ 本机没装 .NET SDK,无法在这里编译验证;XAML/manifest/csproj 已做 XML 良构校验。
118 lines
3.7 KiB
C#
118 lines
3.7 KiB
C#
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Text;
|
|
|
|
namespace YuncangTrace.LocalService.Services;
|
|
|
|
/// <summary>
|
|
/// 单实例 + 参数转发(命名管道)
|
|
///
|
|
/// 场景:窗口已经开着,用户又点了一次 WMS 页面上的「查看打包视频」→ 系统按协议再启动一次本程序。
|
|
/// 这时第二个实例**不应该**去抢 17311 端口,而是把 `yuncang://open?code=xxx` 通过管道送给第一个实例,
|
|
/// 由它把窗口激活并查询那个运单号。
|
|
/// </summary>
|
|
public sealed class SingleInstance : IDisposable
|
|
{
|
|
private readonly Mutex _mutex;
|
|
private readonly string _pipeName;
|
|
private readonly Action<string[]> _onArgs;
|
|
private readonly CancellationTokenSource _cts = new();
|
|
private readonly bool _ownsMutex;
|
|
|
|
/// <summary>本次是否是第一个实例(不是的话调用 <see cref="SendArgs"/> 后就可以退出了)</summary>
|
|
public bool IsFirstInstance { get; }
|
|
|
|
public SingleInstance(string name, Action<string[]> onArgs)
|
|
{
|
|
_onArgs = onArgs;
|
|
_pipeName = $"{name}.pipe";
|
|
|
|
// 全局命名互斥体:跨会话也能认出来(同一个用户下就够了,用 Local\ 前缀)
|
|
_mutex = new Mutex(initiallyOwned: true, $@"Local\{name}.mutex", out var createdNew);
|
|
_ownsMutex = createdNew;
|
|
IsFirstInstance = createdNew;
|
|
}
|
|
|
|
/// <summary>第一个实例调用:开一个后台线程等第二个实例送参数过来</summary>
|
|
public void StartListening()
|
|
{
|
|
var thread = new Thread(ListenLoop)
|
|
{
|
|
IsBackground = true,
|
|
Name = "single-instance-pipe",
|
|
};
|
|
thread.Start();
|
|
}
|
|
|
|
/// <summary>第二个实例调用:把命令行参数送给已运行的实例(连不上就算了,不影响退出)</summary>
|
|
public void SendArgs(string[] args)
|
|
{
|
|
if (args.Length == 0) return;
|
|
|
|
try
|
|
{
|
|
using var client = new NamedPipeClientStream(".", _pipeName, PipeDirection.Out);
|
|
client.Connect(1500);
|
|
|
|
using var writer = new StreamWriter(client, new UTF8Encoding(false)) { AutoFlush = true };
|
|
foreach (var arg in args) writer.WriteLine(arg);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// 拿不到第一个实例(可能正在退出):静默忽略,用户重新点一次即可
|
|
}
|
|
}
|
|
|
|
private void ListenLoop()
|
|
{
|
|
while (!_cts.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
using var server = new NamedPipeServerStream(
|
|
_pipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances,
|
|
PipeTransmissionMode.Byte, PipeOptions.None);
|
|
|
|
server.WaitForConnection();
|
|
|
|
var args = new List<string>();
|
|
using (var reader = new StreamReader(server, new UTF8Encoding(false)))
|
|
{
|
|
string? line;
|
|
while ((line = reader.ReadLine()) is not null)
|
|
{
|
|
if (line.Length > 0) args.Add(line);
|
|
}
|
|
}
|
|
|
|
if (args.Count > 0) _onArgs(args.ToArray());
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (_cts.IsCancellationRequested) return;
|
|
Thread.Sleep(200);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_cts.Cancel();
|
|
|
|
if (_ownsMutex)
|
|
{
|
|
try
|
|
{
|
|
_mutex.ReleaseMutex();
|
|
}
|
|
catch (ApplicationException)
|
|
{
|
|
// 不持有互斥体(理论上不会发生)
|
|
}
|
|
}
|
|
|
|
_mutex.Dispose();
|
|
_cts.Dispose();
|
|
}
|
|
}
|