1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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
- {
- srvcobj = WebApp.Start<Startup>(baseUrl);
- MessageBox.Show($"江科HIS读卡服务已成功启动。\n服务地址: {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);
- }
- throw;
- }
- }
- public void Stop()
- {
- if (srvcobj != null)
- srvcobj.Dispose();
- }
- }
- }
|