1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using Microsoft.Owin.Hosting;
- using System;
- using System.Windows.Forms;
- namespace ThCardReader
- {
- class RestService
- {
- private IDisposable srvcobj;
- public void Start()
- {
- string baseUrl = "http://localhost:8321"; // 保持原端口8321
- try
- {
- // 优先尝试同时绑定所有网卡与 localhost,确保既能用 IP 又能用 localhost 访问
- var options = new StartOptions();
- options.Urls.Add("http://+:8321");
- options.Urls.Add(baseUrl);
- srvcobj = WebApp.Start<Startup>(options);
- MessageBox.Show($"江科HIS读卡服务已成功启动。\n服务地址: http://<本机IP>:8321 / {baseUrl}", "服务启动成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- // 检查是否是端口占用问题
- if (ex.Message.Contains("8321") || ex.Message.Contains("微信") || ex.Message.Contains("冲突"))
- {
- var result = MessageBox.Show(
- $"端口8321被占用(可能是微信等程序)!\n\n" +
- $"错误信息: {ex.Message}\n\n" +
- $"解决方案:\n" +
- $"1. 关闭微信等占用端口的程序\n" +
- $"2. 以管理员身份运行本程序\n" +
- $"3. 重启计算机释放端口\n\n" +
- $"是否打开端口检查工具?",
- "端口占用冲突",
- MessageBoxButtons.YesNo,
- MessageBoxIcon.Warning);
- if (result == DialogResult.Yes)
- {
- try
- {
- System.Diagnostics.Process.Start("check_port.bat");
- }
- catch
- {
- MessageBox.Show("无法启动端口检查工具,请手动运行 check_port.bat", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- }
- else
- {
- MessageBox.Show($"读卡服务启动失败!\n\n错误信息: {ex.Message}\n\n建议解决方案:\n1. 检查端口8321是否被占用\n2. 以管理员身份运行程序\n3. 检查防火墙设置", "服务启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- // // 若绑定所有网卡失败(例如缺少URLACL权限),降级仅监听 localhost,保证原有流程可用
- // try
- // {
- // srvcobj = WebApp.Start<Startup>(baseUrl);
- // MessageBox.Show($"江科HIS读卡服务已以兼容模式启动(仅本机访问)。\n服务地址: {baseUrl}\n\n如需IP访问,请以管理员运行并执行URLACL与防火墙放行。", "服务启动成功(兼容模式)", MessageBoxButtons.OK, MessageBoxIcon.Information);
- // return;
- // }
- // catch
- // {
- // MessageBox.Show($"读卡服务启动失败!\n\n错误信息: {ex.Message}\n\n建议解决方案:\n1. 检查端口8321是否被占用\n2. 以管理员身份运行程序\n3. 执行URLACL与防火墙放行", "服务启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- // }
- }
- throw;
- }
- }
- public void Stop()
- {
- if (srvcobj != null)
- srvcobj.Dispose();
- }
- }
- }
|