Blazor ProtectedSessionStorage 不起作用

Blazor ProtectedSessionStorage doesn't work

我尝试在某些 Blazor 组件(.NetCore 6、Blazor Server)中实现 ProtectedSessionStorage。

我也关注了这个线程:,但是当我尝试设置会话数据时,代码停止并立即退出他的范围。

这是我的代码:

SessionService.cs

using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
...

    public class SessionService
    {
        private ProtectedSessionStorage _sessionStorage;

        public SessionService(ProtectedSessionStorage storage)
        {
            _sessionStorage = storage;
        }

        public async Task<User> GetUserProperty()
        {
            var value = await _sessionStorage.GetAsync<User>("user");
            return value.Value;
        }

        public async Task SetUserProperty(User value)
        {
            await _sessionStorage.SetAsync("user", value);
        }
    }

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    //registering the service
    services.addScoped<SessionService>();
    ...
}

MyComponent.razor

@code
{
   [Inject] public SessionService SessionService { get; set; } = default!;

   protected override async void OnInitialized()
   {
       InitUser();
       LoadInterface();
   }

   protected async Task InitUser()
   {
      ...
      try
      {
          User user = new User("id", "location"); 
          //after this row, code exit from this scope
          await SessionService.SetUserProperty(user);

          //this part of code aren't read
          if(..)
          {
             //some other check
          }
          ...
      }
      catch(Exception ex)
      {
          //No exceptions caught
      }
      
   }
{

我错了什么?


更新:

我忘记了 onInitialized 方法上的异步,现在代码不会停止也不会退出他的范围。但不幸的是,我在浏览器控制台上收到此错误。

blazor.server.js:1 
        
       [2022-02-09T17:11:25.128Z] Error: System.InvalidOperationException: Each parameter in the deserialization constructor on type 'MyProject.Models.Shared.User' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(Type parentType)
   at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedBrowserStorage.Unprotect[TValue](String purpose, String protectedJson)
   at Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage.ProtectedBrowserStorage.GetAsync[TValue](String purpose, String key)
   at MyProject.Services.Dashboard.SessionService.GetUserProperty() in C:\Source\MyProject\Services\Dashboard\SessionService.cs:line 18
   at MyProject.Shared.DashHeaderMenu.OnInitialized() in C:\Source\MyProject\Shared\DashHeaderMenu.razor:line 128
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_0(Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteSynchronously(TaskCompletionSource`1 completion, SendOrPostCallback d, Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<.cctor>b__23_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.ExecuteBackground(WorkItem item)

已解决!

当您尝试插入一个对象时,例如自定义对象 class(在我的例子中是用户),您需要添加一个空的构造函数用于序列化并放入受保护的会话存储中。

public class User
{
   public string name;
   public string surname;
   public int id;
   .....

   //required for serialization
   public User(){}

   //others constructor used
   public User(string name, string surname, ...)
   {
       ...
   }
}

所以,为了解决我所有的问题,我将 async 插入 OnInitialized() 并向 User 添加一个空构造函数 class