我嘗試在一些 Blazor 組件(.NetCore 6、Blazor Server)中實作 ProtectedSessionStorage。
我也關注了這個執行緒:Blazor ProtectedSessionStorage object NULL,但是當我嘗試設定會話資料時,代碼停止并立即退出他的范圍。
這是我的代碼:
會話服務.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);
}
}
啟動.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)
uj5u.com熱心網友回復:
解決了!
當您嘗試插入一個物件時,例如自定義類(在我的例子中是用戶),您需要添加一個空的建構式進行序列化并放入受保護的會話存盤中。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429667.html
標籤:C# 会议 .net-core 西装外套 blazor 服务器端
上一篇:Asp.Netcore6會話密鑰
下一篇:php子域會話共享無法正常作業
