using Coravel; using Coravel.Scheduling.Schedule; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using VideoAnalysisCore.Job; using Microsoft.Extensions.Hosting; using VideoAnalysisCore.Common; using System.Diagnostics; namespace VideoAnalysisCore.Common.Expand { public static class ServiceSystemExpand { /// /// 系统服务 /// /// public static void UseServiceSystem(this IHost app1,Action? action=null) { var app = app1.Services; // 注册启动后的回调 app.GetRequiredService() .ApplicationStarted.Register(() => { var server = AppCommon.Services.GetRequiredService(); var addressFeature = server.Features.Get(); Console.WriteLine("==========================================="); Console.WriteLine($"Kestrel 监听地址: "); foreach (var address in addressFeature.Addresses) { Console.WriteLine($"{address}"); } Console.WriteLine("==========================================="); // 将 "*" 替换为 "localhost" 或 "127.0.0.1",因为 Uri 类不直接支持带有通配符的 Host var normalizedAddress = addressFeature.Addresses.First() .Replace("*", "127.0.0.1") .Replace("+", "127.0.0.1"); var uri = new Uri(normalizedAddress); int port = uri.Port; // 这里的 port 就是你要的数字 (int) if (OperatingSystem.IsWindows()) OpenBrowser($"http://localhost:{uri.Port}/ui/index.html"); if(action != null) action(); }); } // 跨平台打开浏览器的方法 static void OpenBrowser(string url) { try { if (OperatingSystem.IsWindows()) { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); } else if (OperatingSystem.IsLinux()) { Process.Start("xdg-open", url); } else if (OperatingSystem.IsMacOS()) { Process.Start("open", url); } } catch (Exception ex) { Console.WriteLine($"无法自动打开浏览器: {ex.Message}"); } } } }