using System.Diagnostics; using System.Reflection; using MegghysAPI.Attributes; using MegghysAPI.Components; using AntDesign; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddAntDesign(); builder.Services.AddControllers(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseAntiforgery(); app.MapStaticAssets(); app.MapRazorComponents() .AddInteractiveServerRenderMode(); app.UseRouting(); app.UseAntiforgery(); app.MapControllers(); // 加载所有有 AutoInitAttribute 的类 // 获取当前Assembly var inits = new List(); foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) .Where(m => m.GetCustomAttribute() is not null)) { inits.Add(method); } } DB.InitDB(); inits = inits .OrderBy(m => m.GetCustomAttribute()?.Order ?? 0) .ToList(); foreach (var method in inits) { var sw = Stopwatch.StartNew(); var attr = method.GetCustomAttribute(); if (attr?.LogMessage is { } message) { Logs.Info(message); } if (attr?.Async is true) { _ = Task.Run(() => { method.Invoke(null, null); attr.PostInit?.Invoke(); Logs.Info($"[{sw.ElapsedMilliseconds} ms] Async <{method.DeclaringType?.Name}.{method.Name}> => Inited."); }); } else { method.Invoke(null, null); attr?.PostInit?.Invoke(); Logs.Info($"[{sw.ElapsedMilliseconds} ms] <{method.DeclaringType?.Name}.{method.Name}> => Inited."); } } app.Run($"http://{Config.Instance.ListenHost}:{Config.Instance.ListenPort}");