using System.Diagnostics; using System.Reflection; using MegghysAPI.Attributes; using MegghysAPI.Components; using Microsoft.FluentUI.AspNetCore.Components; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddFluentUIComponents(); 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.MapControllers(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); // 加载所有有 AutoInitAttribute 的类 // 获取当前Assembly var inits = new List(); Assembly.GetExecutingAssembly() .GetTypes() .ForEach(t => t.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public) .Where(m => m.GetCustomAttribute() is { }).ForEach(m => inits.Add(m))); inits = [.. inits.OrderBy(m => m.GetCustomAttribute().Order)]; inits.ForEach(m => { var sw = new Stopwatch(); sw.Start(); var attr = m.GetCustomAttribute(); if (attr.LogMessage is not null) Logs.Info(attr.LogMessage); if (attr.Async) { Task.Run(() => { m.Invoke(null, null); attr.PostInit?.Invoke(); Logs.Info($"[{sw.ElapsedMilliseconds} ms] Async <{m.DeclaringType.Name}.{m.Name}> => Inited."); }); } else { m.Invoke(null, null); attr.PostInit?.Invoke(); Logs.Info($"[{sw.ElapsedMilliseconds} ms] <{m.DeclaringType.Name}.{m.Name}> => Inited."); } }); app.Run();