Blazored / LocalStorage

A library to provide access to local storage in Blazor applications
https://blazored.github.io/LocalStorage/
MIT License
1.25k stars 117 forks source link

Should the nuget package work for a .Net 7.0.2 Blazor hosted app when using the Edge browser? #202

Closed PlanBernhard closed 1 year ago

PlanBernhard commented 1 year ago

Hello experts!

Unfortunately I am unable to use this package to store a List in the LocalStorage in the Microsoft Edge browser. Using

(The razor file)

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code{
protected override async Task OnInitializedAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }
}

Doesn't store anything. And using await _localStorage.GetItemAsync<List<CartItem>>("cart"); produces this error

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: 'c' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: 'c' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: 'c' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
   at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1[[System.Collections.Generic.List`1[[BlazorEcommerce.Shared.CartItem, BlazorEcommerce.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1[[System.Collections.Generic.List`1[[BlazorEcommerce.Shared.CartItem, BlazorEcommerce.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[List`1](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[List`1](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[List`1](String json, JsonSerializerOptions options)
   at Blazored.LocalStorage.Serialization.SystemTextJsonSerializer.Deserialize[List`1](String data)
   at Blazored.LocalStorage.LocalStorageService.<GetItemAsync>d__5`1[[System.Collections.Generic.List`1[[BlazorEcommerce.Shared.CartItem, BlazorEcommerce.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()

CartItem model:

public class CartItem
{
    public int ProductId { get; set; }
    public int ProductTypeId { get; set; }
}

But it did work in Google Chrome

image

EDIT: Okay, that is strange. Chrome does only work, if the Visual Studio has not started it in debug mode. The same goes for the dotnet command. If the dotnet watch was executed the storage did not work, image

but with dotnet run it did work. I cleaned an rebuilded my solution now over 10 times. And I also cleaned the whole application storage on all browsers. I don't know what is going on here.

EDIT 2: I restarted my machine, deleted every cache or binary and tried it again. The same result.

PlanBernhard commented 1 year ago

Okay, sorry. It is there. I wrote a method to output the storage. The issue seems to be the display of the devtools.