这是我的blazor wasm项目的配置
builder.Services.AddOptions();
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<AuthStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<AuthStateProvider>());
builder.Services.AddScoped<AuthenticationService>(s => webcontext.Authentication);
builder.Services.AddTransient<AuthMessageHandler>();
builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = AppModelContext.BaseUri).AddHttpMessageHandler<AuthMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ServerAPI"));
和登录页面
[Inject]
public IHttpClientFactory ClientFactory { get; set; }
private async void Login()
{
try
{
var Client = ClientFactory.CreateClient("ServerAPI");
var LoginResult = await Client.GetAsync("WebApis/Login/UserName=e.vali&Password=111");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}
当我运行项目时,
var Client = ClientFactory.CreateClient("ServerAPI");
行 抛出该异常
Unhandled Exception:System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider' while attempting to activate 'AppModel.Services.Authentication.AuthMessageHandler'.
如何注册IAccesstokenProvider服务?