79 lines
3.0 KiB
C#
79 lines
3.0 KiB
C#
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)注册一个专用 HttpClient(BaseAddress 指向应用根)
|
||
// 该 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 时,传入可能存在的 IHttpContextAccessor(Server 提供,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();
|