Blazor Preview 9 问题与 Razor 布局中的记录器
Blazor Preview 9 Issue with Logger in Razor Layout
自升级到 Net Core 3.0 Preview 9
后,我在尝试注入控制台记录器时遇到以下错误;
WASM: System.MissingMethodException: Method not found: System.Threading.Tasks.Task`1 Microsoft.JSInterop.IJSRuntime.InvokeAsync(string,object[])
WASM: at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) <0x2df6378 + 0x0003e> in :0
WASM: at Blazor.Extensions.Logging.BrowserConsoleLogger.Log[TState] (Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception exception, System.Func`3[T1,T2,TResult] formatter) <0x2df5e90 + 0x00094> in <3eb34b93cd4a47bf804fb0648b089edf>:0
WASM: at Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass4_0.b__0 (Microsoft.Extensions.Logging.ILogger logger, System.Exception exception) <0x2df51a0 + 0x00058> in :0
WASM: at (wrapper delegate-invoke) System.Action`2[Microsoft.Extensions.Logging.ILogger,System.Exception].invoke_void_T1_T2(Microsoft.Extensions.Logging.ILogger,System.Exception)
WASM: at Microsoft.Extensions.Logging.LoggingExtensions.UserAuthorizationFailed (Microsoft.Extensions.Logging.ILogger logger) <0x2dee5f0 + 0x00014> in <85acc2b572f448f39259eac9936732f8>:0
WASM: at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync (System.Security.Claims.ClaimsPrincipal user, System.Object resource, System.Collections.Generic.IEnumerable`1[T] requirements) <0x2ddd378 + 0x00338> in <85acc2b572f448f39259eac9936732f8>:0
WASM: at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync (System.Security.Claims.ClaimsPrincipal user) <0x2d52860 + 0x00228> in <5266bbb196bb40a89b886a09631725e6>:0
WASM: at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync () <0x2d512a0 + 0x0026c> in <5266bbb196bb40a89b886a09631725e6>:0
WASM: at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x2e0b8f0 + 0x00118> in :0
WASM: at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2bbb2e8 + 0x00248> in :0
在我的剃刀视图中,我按如下方式注入记录器;
@inject ILogger<Index> logger
我的基地_Imports.razor
如下;
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Authorization
@using System.Net.Http
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using Microsoft.Extensions.Logging
@using Blazored.LocalStorage
@using Blazorise
@using Blazorise.Sidebar
我的 App.razor
是;
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<p>Nope, nope!</p>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
此时呈现的视图是登录页面。如果用户未获得授权,则会在重定向时移至该位置。所以在我的 MainLayout.Razor
页面中,我有以下内容;
@functions
{
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (!user.Identity.IsAuthenticated)
{
UriHelper.NavigateTo(@"\login");
}
}
}
为了完整起见,我的 Startup.cs
中的 ConfigureServices
添加了如下记录器;
services.AddLogging(builder => builder
.AddBrowserConsole()
.SetMinimumLevel(LogLevel.Trace)
);
谁能告诉我我做错了什么?由于 'login' 页面正在呈现,它似乎只是问题所在的记录器?
Per Tsengs 评论我的 nuget 依赖项如下;
您似乎在项目中混合了版本(preview8 和 preview9),或者(第三方)依赖于针对 preview8
.
构建的库
在预览版 9 IJSRuntime
中,签名从 Task<T>
更改为 ValueTask<T>
来源:https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-9/
- Update implementations of IJSRuntime to return ValueTask.
如果是第三方库,请升级到最新版本或等待库的开发者更新。
自升级到 Net Core 3.0 Preview 9
后,我在尝试注入控制台记录器时遇到以下错误;
WASM: System.MissingMethodException: Method not found: System.Threading.Tasks.Task`1 Microsoft.JSInterop.IJSRuntime.InvokeAsync(string,object[])
WASM: at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) <0x2df6378 + 0x0003e> in :0
WASM: at Blazor.Extensions.Logging.BrowserConsoleLogger.Log[TState] (Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception exception, System.Func`3[T1,T2,TResult] formatter) <0x2df5e90 + 0x00094> in <3eb34b93cd4a47bf804fb0648b089edf>:0
WASM: at Microsoft.Extensions.Logging.LoggerMessage+<>c__DisplayClass4_0.b__0 (Microsoft.Extensions.Logging.ILogger logger, System.Exception exception) <0x2df51a0 + 0x00058> in :0
WASM: at (wrapper delegate-invoke) System.Action`2[Microsoft.Extensions.Logging.ILogger,System.Exception].invoke_void_T1_T2(Microsoft.Extensions.Logging.ILogger,System.Exception)
WASM: at Microsoft.Extensions.Logging.LoggingExtensions.UserAuthorizationFailed (Microsoft.Extensions.Logging.ILogger logger) <0x2dee5f0 + 0x00014> in <85acc2b572f448f39259eac9936732f8>:0
WASM: at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync (System.Security.Claims.ClaimsPrincipal user, System.Object resource, System.Collections.Generic.IEnumerable`1[T] requirements) <0x2ddd378 + 0x00338> in <85acc2b572f448f39259eac9936732f8>:0
WASM: at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync (System.Security.Claims.ClaimsPrincipal user) <0x2d52860 + 0x00228> in <5266bbb196bb40a89b886a09631725e6>:0
WASM: at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync () <0x2d512a0 + 0x0026c> in <5266bbb196bb40a89b886a09631725e6>:0
WASM: at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x2e0b8f0 + 0x00118> in :0
WASM: at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2bbb2e8 + 0x00248> in :0
在我的剃刀视图中,我按如下方式注入记录器;
@inject ILogger<Index> logger
我的基地_Imports.razor
如下;
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Authorization
@using System.Net.Http
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using Microsoft.Extensions.Logging
@using Blazored.LocalStorage
@using Blazorise
@using Blazorise.Sidebar
我的 App.razor
是;
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<p>Nope, nope!</p>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
此时呈现的视图是登录页面。如果用户未获得授权,则会在重定向时移至该位置。所以在我的 MainLayout.Razor
页面中,我有以下内容;
@functions
{
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (!user.Identity.IsAuthenticated)
{
UriHelper.NavigateTo(@"\login");
}
}
}
为了完整起见,我的 Startup.cs
中的 ConfigureServices
添加了如下记录器;
services.AddLogging(builder => builder
.AddBrowserConsole()
.SetMinimumLevel(LogLevel.Trace)
);
谁能告诉我我做错了什么?由于 'login' 页面正在呈现,它似乎只是问题所在的记录器?
Per Tsengs 评论我的 nuget 依赖项如下;
您似乎在项目中混合了版本(preview8 和 preview9),或者(第三方)依赖于针对 preview8
.
在预览版 9 IJSRuntime
中,签名从 Task<T>
更改为 ValueTask<T>
来源:https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-9/
- Update implementations of IJSRuntime to return ValueTask.
如果是第三方库,请升级到最新版本或等待库的开发者更新。