Files
Atomx/Atomx.Admin/Atomx.Admin.Client/Program.cs
2025-12-14 18:27:21 +08:00

79 lines
3.0 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 Atomx.Admin.Client.Validators;
using Blazored.LocalStorage;
using FluentValidation;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Localization;
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) });
// 注册 LocalizationProvider (用于 WASM)
// Use Scoped lifetime because LocalizationProvider depends on IJSRuntime (scoped) and IHttpClient etc.
builder.Services.AddScoped<ILocalizationProvider, LocalizationProvider>();
// 注册 ILocalizationService 用于同步 Culture 在组件间传播
builder.Services.AddScoped<ILocalizationService, LocalizationService>();
// 注入 IStringLocalizer<T> 实现
builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(JsonStringLocalizer<>));
// 添加本地化服务
builder.Services.AddLocalization();
// 注册用于自动附带 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.AddValidatorsFromAssembly(typeof(LoginModelValidator).Assembly);
builder.Services.AddAntDesign();
var host = builder.Build();
await host.RunAsync();