Files
Atomx/Atomx.Admin/Atomx.Admin.Client/Program.cs
2025-12-06 13:30:17 +08:00

73 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();
// 权限 & 本地化(客户端实现)
builder.Services.AddScoped<IPermissionService, ClientPermissionService>();
builder.Services.AddScoped<IconsExtension>();
// 为静态资源wwwroot注册一个专用 HttpClientBaseAddress 指向应用根)
// 该 HttpClient 用于加载 wwwroot/Localization/{culture}.json 等静态语言文件
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// 注册本地化工厂(使用相对于站点根的目录名 "Localization" 并注入用于加载静态文件的 HttpClient
builder.Services.AddScoped<IStringLocalizerFactory>(sp =>
new JsonStringLocalizerFactory("Localization", sp.GetRequiredService<HttpClient>()));
// 注册IStringLocalizer
builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
// 添加本地化服务
builder.Services.AddLocalization();
// 注册语言提供者
builder.Services.AddScoped<LanguageProvider>();
// 注册用于自动附带 token & 刷新的 DelegatingHandler
builder.Services.AddScoped<AuthHeaderHandler>();
// 命名 HttpClient应用内统一使用 ApiClient并将 AuthHeaderHandler 加入请求管道
var apiBase = builder.Configuration["WebApi:ServerUrl"] ?? builder.HostEnvironment.BaseAddress;
builder.Services.AddHttpClient("ApiClient", client =>
{
client.BaseAddress = new Uri(apiBase);
})
.AddHttpMessageHandler<AuthHeaderHandler>();
// 注册一个不带 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<IHttpClientFactory>().CreateClient("ApiClient"));
// 在 WASM 的 Program.cs客户端中注册 HttpService 时,传入可能存在的 IHttpContextAccessorServer 提供WASM 为 null
builder.Services.AddScoped<HttpService>(sp =>
{
var httpClient = sp.GetRequiredService<HttpClient>();
var httpContextAccessor = sp.GetService<IHttpContextAccessor>();
return new HttpService(httpClient, httpContextAccessor);
});
builder.Services.AddAntDesign();
await builder.Build().RunAsync();