Blazor WASM新增:Crit错误未处理异常呈现组件
我是Blazor的新手,但我有一个可路由的组件。它调用允许匿名的api端点。据我所知,Blaozr WASM会自动将头中的令牌附加到每个http请求,因此,在我的Blazor WASM HttpService中,我有两个httpClient设置,一个用于匿名,一个用于调用令牌的http 以登录用户的身份转到路由工作正常,未登录就转到路由会产生以下错误,但我不确定原因。此外,我还尝试在所有相关部分(http服务、组件类)放置断点,但没有一个断点被命中。我在组件中的Blazor WASM新增:Crit错误未处理异常呈现组件,blazor,blazor-webassembly,Blazor,Blazor Webassembly,我是Blazor的新手,但我有一个可路由的组件。它调用允许匿名的api端点。据我所知,Blaozr WASM会自动将头中的令牌附加到每个http请求,因此,在我的Blazor WASM HttpService中,我有两个httpClient设置,一个用于匿名,一个用于调用令牌的http 以登录用户的身份转到路由工作正常,未登录就转到路由会产生以下错误,但我不确定原因。此外,我还尝试在所有相关部分(http服务、组件类)放置断点,但没有一个断点被命中。我在组件中的user.Identity.isA
user.Identity.isAuthenticated
处放置了一个断点,但它仍然没有被命中。页面只是在控制台中加载该错误。有什么指导吗?我不知道如果断点不被尊重,我怎么能有效地调试它,哈哈
Program.CS有:
builder.Services.AddHttpClient("Quillerz.AnonAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddHttpClient("Quillerz.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddHttpClient(“Quillerz.AnonAPI”,client=>client.BaseAddress=newURI(builder.hostenEnvironment.BaseAddress))
.AddHttpMessageHandler();
builder.Services.AddHttpClient(“Quillerz.ServerAPI”,client=>client.BaseAddress=新Uri(builder.hostenEnvironment.BaseAddress))
.AddHttpMessageHandler();
组件代码:
@page "/communities/{CommunityId:guid}"
@using Quillerz.Shared.Communities
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject Quillerz.Client.Services.ICommunityHttpService CommunityService
@inject NavigationManager Navigation
@inject NotificationService NotificationService
@if (Community != null)
{
<div class="container">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<div class="d-flex w-100 justify-content-between">
<h5 class="card-title">@Community.Name</h5>
<span class="text-muted">@Community.MemberCount members</span>
</div>
</div>
</div>
</div>
</div>
</div>
}
else
{
<span>Couldn't find community.</span>
}
@code {
[Parameter]
public Guid CommunityId { get; set; }
private CommunityDto Community { get; set; } = new CommunityDto();
protected override async Task OnInitializedAsync()
{
if (!string.IsNullOrEmpty(CommunityId.ToString()))
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
Community = await CommunityService.GetCommunity(CommunityId, true);
}
else
{
Community = await CommunityService.GetCommunity(CommunityId, false);
}
}
else
{
NotificationMessage errMsg = new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Whoops!",
Detail = "Couldn't find the community id.",
Duration = 4000
};
NotificationService.Notify(errMsg);
}
}
}
@page”/communities/{CommunityId:guid}
@使用Quillerz.Shared.Communities
@使用Microsoft.AspNetCore.Components.WebAssembly.Authentication
@注入AuthenticationStateProvider AuthenticationStateProvider
@注入Quillerz.Client.Services.ICommunityHttpService社区服务
@注入导航管理器导航
@注入NotificationService NotificationService
@if(社区!=null)
{
@社区名称
@Community.MemberCount成员
}
其他的
{
找不到社区。
}
@代码{
[参数]
公共Guid社区ID{get;set;}
private CommunityDto Community{get;set;}=new CommunityDto();
受保护的重写异步任务OnInitializedAsync()
{
如果(!string.IsNullOrEmpty(CommunityId.ToString()))
{
var authState=等待AuthenticationStateProvider.GetAuthenticationStateAync();
var user=authState.user;
if(user.Identity.IsAuthenticated)
{
Community=wait CommunityService.GetCommunity(CommunityId,true);
}
其他的
{
Community=wait CommunityService.GetCommunity(CommunityId,false);
}
}
其他的
{
NotificationMessage errMsg=新建NotificationMessage()
{
严重性=通知严重性。错误,
Summary=“哎呀!”,
Detail=“找不到社区id”。”,
持续时间=4000
};
NotificationService.Notify(errMsg);
}
}
}
HttpService:
private readonly HttpClient httpClient;
private readonly IHttpClientFactory httpClientFactory;
private readonly HttpClient anonHttp;
public CommunityHttpService(HttpClient httpClient, IHttpClientFactory httpClientFactory)
{
if (httpClient == null) throw new ArgumentNullException("Http is null.");
this.httpClient = httpClient;
this.httpClientFactory = httpClientFactory;
anonHttp = httpClientFactory.CreateClient("Quillerz.AnonAPI");
}
public async Task<CommunityDto> GetCommunity(Guid communityId, bool userIsAuthenticated)
{
if (!userIsAuthenticated)
{
return await anonHttp.GetFromJsonAsync<CommunityDto>($"api/communities/" + communityId);
}
else
{
return await httpClient.GetFromJsonAsync<CommunityDto>($"api/communities/" +
communityId);
}
}
私有只读HttpClient HttpClient;
私有只读IHttpClientFactory httpClientFactory;
私有只读HttpClient anonHttp;
公共社区HttpService(HttpClient HttpClient,IHttpClient工厂httpClientFactory)
{
如果(httpClient==null)抛出新的ArgumentNullException(“Http为null”);
this.httpClient=httpClient;
this.httpClientFactory=httpClientFactory;
anonHttp=httpClientFactory.CreateClient(“Quillerz.AnonAPI”);
}
公共异步任务GetCommunity(Guid communityId,bool userIsAuthenticated)
{
如果(!userIsAuthenticated)
{
返回wait await anonHttp.GetFromJsonAsync($“api/communities/”+communityId);
}
其他的
{
返回wait wait httpClient.GetFromJsonAsync($“api/communities/”+
社区ID);
}
}
控制台中的完全错误:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: ''
Microsoft.AspNetCore.Components.WebAssembly.Authentication.AccessTokenNotAvailableException: ''
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
at System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__9`1[[Quillerz.Shared.Communities.CommunityDto, Quillerz.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Quillerz.Client.Services.CommunityHttpService.GetCommunity(Guid communityId, Boolean userIsAuthenticated) in C:\Code\Quillerz\Quillerz\Quillerz\Client\Services\CommunityHttpService.cs:line 45
at Quillerz.Client.Pages.CommunityDetail.OnInitializedAsync() in C:\Code\Quillerz\Quillerz\Quillerz\Client\Pages\CommunityDetail.razor:line 51
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
blazor.webassembly.js:1 crit:Microsoft.AspNetCore.Components.webassembly.Rendering.WebAssemblyRenderer[100]
未处理的异常呈现组件:“”
Microsoft.AspNetCore.Components.WebAssembly.Authentication.AccessTokenNotAvailableException:“”
位于Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
位于Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
在System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage请求、HttpCompletionOption completionOption、Boolean异步、Boolean emitTelemetryStartStop、CancellationToken CancellationToken)
在System.Net.Http.Json.HttpClientJsonExtensions.d_u9`1[[Quillerz.Shared.Communities.CommunityDto,Quillerz.Shared,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null].MoveNext()
位于C:\Code\Quillerz\Quillerz\Quillerz\Quillerz\Client\Services\CommunityHttpService.GetCommunity中的Quillerz.Client.Services.CommunityHttpService.GetCommunity(Guid communityId,布尔用户已验证):第45行
在C:\Code\Quillerz\Quillerz\Quillerz\Quillerz\Client\Pages\CommunityDetail.OnInitializedAsync()中
在Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()中
位于Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)