4.0.0 SetItemAsync can no longer store raw string
See original GitHub issueDescribe the bug It appears there is an undocumented breaking change in 4.0.0 to the SetItemAsync function. In 3.0 you could pass a string directly to this method and it would be stored without being json encoded.
To Reproduce
This snippet demostrates the issue
var userRolesJson = await _http.GetStringAsync("api/UserProfile/MySecurityProfile");
//in 3.0 this would store a non json encoded string
//in 4.0 the string is encoded breaking the GetItemAsync statement below that previously worked
await _localStorage.SetItemAsync(SecurityProfileLocalStorageKey, userRolesJson);
//broken in 4.0 worked previously in 3.0
var myProfilePoco = await _localStorage.GetItemAsync<UserSecurityProfile>(SecurityProfileLocalStorageKey);
Looks like the removal of the following statement on line 33 in LocalStorageService.cs in commit 76dbf13f is what caused this behavior
if (data is string)
{
await _jSRuntime.InvokeVoidAsync("localStorage.setItem", key, data).ConfigureAwait(false);
}
Expected behavior Expected behavior would be the interface preserves it existing semantic; e.g the ability to store a non json encoded string.
If the change to the existing semantic was intended then documenting it as a breaking change from 3.0 to 4.0 would probably help avoid further issues being opened on this topic.
Screenshots If applicable, add screenshots to help explain your problem.
Hosting Model (is this issue happening with a certain hosting model?):
- Blazor WebAssembly
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
I agree @groogiam. What I think would be the best solution here is to add an additional method to
ILocalStorageService
, something like this:This would then give us a nice paring with
GetItemAsStringAsync
.Once this was done developers can choose to either have the library handle serialization by using
SetItemAsync
andGetItemAsync
. Or handle serialization themselves by usingSetItemAsStringAsync
andGetItemAsStringAsync
.How does that sound to you?
Just integrated it into my code and it appears to be working without issue. Thanks for all your help and all your great work on this project!