Blazor Preview 9在Razor布局中的记录器问题

时间:2019-09-10 08:00:08

标签: c# asp.net-core blazor blazor-client-side

自升级到Net Core 3.0 Preview 9以来,尝试注入控制台记录器时出现以下错误;

  

WASM:System.MissingMethodException:找不到方法:System.Threading.Tasks.Task`1 Microsoft.JSInterop.IJSRuntime.InvokeAsync(string,object [])

     

WASM:位于System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start [TStateMachine](TStateMachine&stateMachine)<0x2df6378 + 0x0003e>在:0

     

WASM:位于Blazor.Extensions.Logging.BrowserConsoleLogger.Log [TState](Microsoft.Extensions.Logging.LogLevel logLevel,Microsoft.Extensions.Logging.EventId eventId,TState状态,System.Exception异常,System.Func`3 [T1,T2,TResult]格式化程序)<3eb34b93cd4a47bf804fb0648b089edf>:0

中的<0x2df5e90 + 0x00094>      

WASM:在Microsoft.Extensions.Logging.LoggerMessage + <> c__DisplayClass4_0.b__0(Microsoft.Extensions.Logging.ILogger记录器,System.Exception异常)<0x2df51a0 + 0x00058>在:0

     

WASM:在(包装委托调用)System.Action`2 [Microsoft.Extensions.Logging.ILogger,System.Exception] .invoke_void_T1_T2(Microsoft.Extensions.Logging.ILogger,System.Exception)

     

WASM:Microsoft.Extensions.Logging.LoggingExtensions.UserAuthorizationFailed(Microsoft.Extensions.Logging.ILogger记录器)<85acc2b572f448f39259eac9936732f8>:0

中的<0x2dee5f0 + 0x00014>      

WASM:在Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(System.Security.Claims.ClaimsPrincipal用户,System.Object资源,System.Collections.Generic.IEnumerable`1 [T]要求)<0x2ddd378 + 0x00338> <85acc2b572f448f39259eac9936732f8>:0

     

WASM:在Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync(System.Security.Claims.ClaimsPrincipal用户)<0x2d52860 + 0x00228>在<5266bbb196196bb40a89b886a09631725e6>:0

     

WASM:在Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()<5266bbb196bb40a89b886a0963172525e6>:0

中的<0x2d512a0 + 0x0026c>      

WASM:位于Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(System.Threading.Tasks.Task任务)中:<0x2e0b8f0 + 0x00118>在:0

     

WASM:位于Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()<0x2bbb2e8 + 0x00248>在: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");
        }
    }
}

为完整起见,我的ConfigureServices中的Startup.cs添加了记录器,如下所示;

        services.AddLogging(builder => builder
           .AddBrowserConsole() 
           .SetMinimumLevel(LogLevel.Trace)
       );

有人可以告诉我我在做什么错吗?随着“登录”页面的呈现,似乎只是问题所在的记录器了?

每位曾梵志评论我的nuget依赖关系如下;

blazor dependencies

1 个答案:

答案 0 :(得分:4)

似乎您在项目中混合使用版本(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/

  
      
  • 更新IJSRuntime的实现以返回ValueTask。
  •   

如果是第三方库,请升级到最新版本,或者等到库的开发人员对其进行更新。