我正在尝试创建一个具有两种布局的应用程序(UnauthorizedLayout-用于未授权用户,MainLayout-针对授权用户(。我在将AuthenticationState作为CascadingValue传递时遇到问题。
应用程序剃刀
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<NotFoundPage />
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
MainLayout.rarzor
@using Newtonsoft.Json
@inherits LayoutComponentBase
<div class="main-page">
<CascadingValue Value="CurrentUser">
<NavMenu />
@Body
</CascadingValue>
</div>
@code {
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
public User CurrentUser { get; set; }
private User GetUser()
{
var auth = authenticationStateTask.Result;
return JsonConvert.DeserializeObject<User>(auth.User.FindFirst("user").Value);
}
protected override void OnInitialized() => CurrentUser = GetUser();
}
然而,我收到以下错误:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Cannot wait on monitors on this runtime.
System.PlatformNotSupportedException: Cannot wait on monitors on this runtime.
at System.Threading.Monitor.ObjWait(Int32 millisecondsTimeout, Object obj)
at System.Threading.Monitor.Wait(Object obj, Int32 millisecondsTimeout)
at System.Threading.ManualResetEventSlim.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.SpinThenBlockingWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.InternalWaitCore(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.InternalWait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task`1[[Microsoft.AspNetCore.Components.Authorization.AuthenticationState, Microsoft.AspNetCore.Components.Authorization, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1[[Microsoft.AspNetCore.Components.Authorization.AuthenticationState, Microsoft.AspNetCore.Components.Authorization, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].get_Result()
at MytyPro.B2B.Shared.MainLayout.GetUser() in D:ProjectsTestSharedMainLayout.razor:line 19
at MytyPro.B2B.Shared.MainLayout.OnInitialized() in D:ProjectsTestSharedMainLayout.razor:line 23
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
和
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at MytyPro.B2B.Shared.NavMenu.GetUser() in D:ProjectsTestSharedNavMenu.razor:line 86
at MytyPro.B2B.Shared.NavMenu.OnInitialized() in D:ProjectsTestSharedNavMenu.razor:line 81
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
另外一个问题是,即使我还没有被授权,这一切都发生在MainLayout视图中。
这样的东西应该可以工作。您处于异步世界中,因此无法将任务与OnInitialized
等同步方法交织在一起。
protected override async Task OnInitializedAsync()
=> CurrentUser = await GetUser();
private async Task<User> GetUser()
{
var auth = await authenticationStateTask;
return auth.User;
}
authenticationStateTask.Result;
正在阻塞并且可能/将导致死锁。