Is session storage supported in Blazor Webassembly (PWA) standalone (Not hosted in asp.net core)?

I have the following code in my class that inherits from AuthenticationStateProvider

public async override Task<AuthenticationState> GetAuthenticationStateAsync()
        {    
            if (sessionStorageService.ContainKeyAsync("UserProfile").Result)
            {
                var mAUser = await sessionStorageService.GetItemAsync<MAUser>("UserProfile");
                return await Task.FromResult(BuildAuthenticationState(mAUser));
            }
            else
            {
                return Anonymous;
            }
        }

And I get the error on line:

if (sessionStorageService.ContainKeyAsync("UserProfile").Result)

And the error is:

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(Boolean exitContext, Int32 millisecondsTimeout, Object obj)
   at System.Threading.Monitor.Wait(Object obj, Int32 millisecondsTimeout, Boolean exitContext)
   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[[System.Boolean, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1[[System.Boolean, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].get_Result()
   at MyAushadhaBlazor.Auth.AuthStateProvider.GetAuthenticationStateAsync() in C:xxxxxxxxxxxxxxxxBlazorApp1AuthAuthStateProvider.cs:line 40
   at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Blazor WASM has compared to Blazor Server limited support for a task scheduler. Things like .GetAwaiter().GetResult() will not work in Blazor WASM. This explains your error message

Cannot wait on monitors on this runtime

If you add an await in front of ContainKeyAsync and remove the .Result it should work.

public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{    
    if (await sessionStorageService.ContainKeyAsync("UserProfile"))
    {
        var mAUser = await sessionStorageService.GetItemAsync<MAUser>("UserProfile");
        return await Task.FromResult(BuildAuthenticationState(mAUser));
    }
    else
    {
        return Anonymous;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x