Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Cant write value with SetItemAsync(), v2.1.4 #65

Closed
daniel-rck opened this issue Mar 31, 2020 · 11 comments · Fixed by #66
Closed

[Bug] Cant write value with SetItemAsync(), v2.1.4 #65

daniel-rck opened this issue Mar 31, 2020 · 11 comments · Fixed by #66
Labels
Bug Something isn't working Resolved: Completed The issue has been resolved

Comments

@daniel-rck
Copy link

Describe the bug
When I try to save a jsonToken with ILocalStorageService.SetItemAsync() a exception is thrown (I hope you can see something in the screenshot). My jsonToken was not saved. Trying GetItemAsync<string>. I will stay to v2.1.3, that version is still working.

To Reproduce
Steps to reproduce the behavior:

  1. Update Blazored.LocalStorage to v2.1.4
  2. Try to save a string value
  3. Catch exception
  4. Try read value, see wrong value.

Screenshots
grafik

Hosting Model (is this issue happening with a certain hosting model?):

  • Blazor WebAssembly

Additional context

    <PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview3.20168.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0-preview3.20168.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0-preview3.20168.3" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0-preview3.20168.3" />
@daniel-rck daniel-rck added Bug Something isn't working Triage Issue needs to be triaged labels Mar 31, 2020
@chrissainty
Copy link
Member

chrissainty commented Mar 31, 2020

Hi @daniel-rck, are you sure it broke with v2.1.4? There were no changes to the SetItem<T> method only to the GetItem<T> method. However, v2.1.3 had changes to the SetItem<T> method.

@chrissainty chrissainty added Needs: More Information The author needs to provide more information on the issue. and removed Triage Issue needs to be triaged labels Mar 31, 2020
@daniel-rck
Copy link
Author

On my side there were also no changes besides the LocalStorage-Nuget.
I tested other string, they were working. In fact only my tyical JWT failed. Can I send you the value that you can reproduce the error? I feel unsafe posting it here.

@chrissainty
Copy link
Member

Yes, please do. Either DM me it on Twitter or there is a contact form on my website via the Speaking page.

@chrissainty
Copy link
Member

@daniel-rck I've used your JWT in the sample app and it has saved to local storage fine, without any errors.

@soltrac
Copy link

soltrac commented Mar 31, 2020

I have exactly the same problem.

For example this, in new version this is not working for me. It was working ok on previous version.

.SetAsync("Blablabla", "6QLE0LL7iw7tHPAwold31qUENt3lVTUZxDGqeXQFx38=");

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] blazor.webassembly.js:1:36074 Unhandled exception rendering component: 'Q' is an invalid end of a number. Expected a delimiter. Path: $ | LineNumber: 0 | BytePositionInLine: 1. blazor.webassembly.js:1:36074 System.Text.Json.JsonException: 'Q' is an invalid end of a number. Expected a delimiter. Path: $ | LineNumber: 0 | BytePositionInLine: 1. ---> System.Text.Json.JsonReaderException: 'Q' is an invalid end of a number. Expected a delimiter. LineNumber: 0 | BytePositionInLine: 1. blazor.webassembly.js:1:36074 at System.Text.Json.ThrowHelper.ThrowJsonReaderException (System.Text.Json.Utf8JsonReader& json, System.Text.Json.ExceptionResource resource, System.Byte nextByte, System.ReadOnlySpan1[T] bytes) <0x2ab0460 + 0x00020> in :0 blazor.webassembly.js:1:36074
at System.Text.Json.Utf8JsonReader.TryGetNumber (System.ReadOnlySpan1[T] data, System.Int32& consumed) <0x2a154a0 + 0x00136> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.Utf8JsonReader.ReadFirstToken (System.Byte first) <0x28771f0 + 0x00160> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.Utf8JsonReader.ReadSingleSegment () <0x2876bb8 + 0x001fe> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.Utf8JsonReader.Read () <0x2876740 + 0x0000e> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x28760b0 + 0x0005e> in <filename unknown>:0 blazor.webassembly.js:1:36074 --- End of inner exception stack trace --- blazor.webassembly.js:1:36074 at System.Text.Json.ThrowHelper.ReThrowWithPath (System.Text.Json.ReadStack& readStack, System.Text.Json.JsonReaderException ex) <0x2b02e80 + 0x00108> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x28760b0 + 0x00292> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.JsonSerializer.ReadCore (System.Type returnType, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader) <0x286d0f8 + 0x0002e> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.JsonSerializer.Deserialize (System.String json, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x286c090 + 0x0017a> in <filename unknown>:0 blazor.webassembly.js:1:36074 at System.Text.Json.JsonSerializer.Deserialize[TValue] (System.String json, System.Text.Json.JsonSerializerOptions options) <0x2a87938 + 0x0000c> in <filename unknown>:0 blazor.webassembly.js:1:36074 at Blazored.LocalStorage.LocalStorageService.GetItemAsync[T] (System.String key) <0x2aaf650 + 0x0018e> in <filename unknown>:0 blazor.webassembly.js:1:36074 at Blazored.LocalStorage.LocalStorageService.RaiseOnChangingAsync (System.String key, System.Object data) <0x2ab7bd0 + 0x000fc> in <filename unknown>:0 blazor.webassembly.js:1:36074 at Blazored.LocalStorage.LocalStorageService.SetItemAsync (System.String key, System.Object data) <0x2ab6bb8 + 0x000fe> in <filename unknown>:0

@soltrac
Copy link

soltrac commented Mar 31, 2020

What i see is that setitemasync is believing that i'm trying to save an int, instead a string.

@chrissainty
Copy link
Member

Ok, so it looks like the changes made to fix #57 and subsequent bug fixes have broken things for others.

I would suggest @soltrac and @daniel-rck go back to an earlier version of the library and I'll have a think about what to do.

@daniel-rck
Copy link
Author

daniel-rck commented Mar 31, 2020

The problem comes from LocalStorageService.cs, Line 183, when RaiseOnChangingAsync calls GetItemAsync<object>(key). Should it be the same T and also GetItemAsync<string>(key) so it's running in the else-clause?

EDIT: I thought about it, maybe a simple try-catch would be enough, when the T of GetItemAsync is object?

@chrissainty
Copy link
Member

chrissainty commented Mar 31, 2020

The problem is that when RaiseOnChangingAsync is called we don't know what T should be. A developer could be overwriting a value with a certain type, and replacing it with a value of a different type.

I've added a new internal version of GetItemAsync which doesn't have the additional check for T not being a string. This seems to have fixed it.

@daniel-rck
Copy link
Author

daniel-rck commented Mar 31, 2020

Yep, removing this condition works for me too. The checks for JSON start and end should be enough imho.

@chrissainty chrissainty added Resolved: Completed The issue has been resolved and removed Needs: More Information The author needs to provide more information on the issue. labels Apr 1, 2020
@chrissainty
Copy link
Member

@daniel-rck @soltrac The issues should be resolved in the latest release. Let me know if you experience any further issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Resolved: Completed The issue has been resolved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants