Files
Atomx/Atomx.Admin/Atomx.Admin.Client/Program.cs
2025-12-04 03:08:29 +08:00

47 lines
1.7 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;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// 注册本地存储(单例)
builder.Services.AddBlazoredLocalStorageAsSingleton();
// 授权/身份相关
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();
// 权限 & 本地化
builder.Services.AddScoped<IPermissionService, ClientPermissionService>();
builder.Services.AddScoped<IconsExtension>();
builder.Services.AddScoped<ILocalizationService, LocalizationClientService>();
// Token providerWASM
builder.Services.AddScoped<ITokenProvider, ClientTokenProvider>();
// 注册用于自动附带 token & 刷新的 DelegatingHandler
builder.Services.AddScoped<AuthHeaderHandler>();
// 注册一个命名 HttpClient供应用中所有 API 请求使用),并将 AuthHeaderHandler 加入管道
var apiBase = builder.Configuration["WebApi:ServerUrl"] ?? builder.HostEnvironment.BaseAddress;
builder.Services.AddHttpClient("ApiClient", client =>
{
client.BaseAddress = new Uri(apiBase);
})
.AddHttpMessageHandler<AuthHeaderHandler>();
// 为方便注入未带名字的 HttpClientAuthHeaderHandler 内部 CreateClient() 使用默认工厂),
// 也注册默认 HttpClient 的 BaseAddress
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ApiClient"));
// 在 WASM DI 中注册 HttpService使用上面注入的 HttpClient 实例
builder.Services.AddScoped<HttpService>(sp => new HttpService(sp.GetRequiredService<HttpClient>()));
builder.Services.AddAntDesign();
await builder.Build().RunAsync();