using Atomx.Admin.Client.Services; using Atomx.Admin.Client.Utils; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Localization; using System.Net.Http; var builder = WebAssemblyHostBuilder.CreateDefault(args); // 注册本地存储(WASM 使用 localStorage 保存 tokens) builder.Services.AddBlazoredLocalStorageAsSingleton(); // 授权/身份(WASM 使用轻量的 AuthenticationStateProvider) builder.Services.AddAuthorizationCore(); builder.Services.AddCascadingAuthenticationState(); builder.Services.AddSingleton(); // 权限 & 本地化(客户端实现) builder.Services.AddScoped(); builder.Services.AddScoped(); // 为静态资源(wwwroot)注册一个专用 HttpClient(BaseAddress 指向应用根) // 该 HttpClient 用于加载 wwwroot/Localization/{culture}.json 等静态语言文件 builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); // 注册本地化工厂(使用相对于站点根的目录名 "Localization" 并注入用于加载静态文件的 HttpClient) builder.Services.AddScoped(sp => new JsonStringLocalizerFactory("localization", sp.GetRequiredService())); // 注册IStringLocalizer builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>)); // 添加本地化服务 builder.Services.AddLocalization(); // 注册语言提供者 builder.Services.AddScoped(); // 注册用于自动附带 token & 刷新的 DelegatingHandler builder.Services.AddScoped(); // 命名 HttpClient(应用内统一使用 ApiClient),并将 AuthHeaderHandler 加入请求管道 var apiBase = builder.Configuration["WebApi:ServerUrl"] ?? builder.HostEnvironment.BaseAddress; builder.Services.AddHttpClient("ApiClient", client => { client.BaseAddress = new Uri(apiBase); }) .AddHttpMessageHandler(); // 注册一个不带 AuthHeaderHandler 的 HttpClient,用于刷新 token(避免循环调用) // 该 client 名称在 AuthHeaderHandler 中使用 "RefreshClient" builder.Services.AddHttpClient("RefreshClient", client => { client.BaseAddress = new Uri(apiBase); }); // 默认 HttpClient(方便注入 HttpClient;此处仍使用命名的 ApiClient) builder.Services.AddScoped(sp => sp.GetRequiredService().CreateClient("ApiClient")); // 在 WASM 的 Program.cs(客户端)中注册 HttpService 时,传入可能存在的 IHttpContextAccessor(Server 提供,WASM 为 null) builder.Services.AddScoped(sp => { var httpClient = sp.GetRequiredService(); var httpContextAccessor = sp.GetService(); return new HttpService(httpClient, httpContextAccessor); }); builder.Services.AddAntDesign(); await builder.Build().RunAsync();