Search code examples
c#asp.net-coreblazorblazor-client-side

Blazor Preview 9 Issue with Logger in Razor Layout


Since upgrading to Net Core 3.0 Preview 9 I have been getting the following error when trying inject my console logger;

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

In my razor view I inject the logger as follows;

@inject ILogger<Index> logger

My base _Imports.razor is as follows;

@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

and my App.razor is;

<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>

Now the view being rendered at this point is a login page. Which is moved to on a redirect if the user is not authorized. So in my MainLayout.Razor page I have the following;

@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");
        }
    }
}

For completeness the ConfigureServices in my Startup.cs adds the logger as follows;

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

Can anyone advise me what I am doing wrong here? As the 'login' page is rendering, it just seems to be the logger where the issue lies?

Per Tsengs comment my nuget dependencies are as follows;

blazor dependencies


Solution

  • Seems you are mixing versions (preview8 and preview9) in your projects or have a (third party) dependency on a library which is built against preview8.

    In preview 9 IJSRuntime has signatures changed from Task<T> to ValueTask<T>

    Source: 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.

    In case of third party libraries, upgrade to newest version or wait till the developer of the library updates it.