question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

4.0.0 SetItemAsync can no longer store raw string

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
chrissaintycommented, May 14, 2021

I agree @groogiam. What I think would be the best solution here is to add an additional method to ILocalStorageService, something like this:

ValueTask SetItemAsStringAsync(string key, string data);

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 and GetItemAsync. Or handle serialization themselves by using SetItemAsStringAsync and GetItemAsStringAsync.

How does that sound to you?

1reaction
groogiamcommented, May 15, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected token Issue - Raw string in R4.0.0
Hello All, I've recently upgraded to R 4.0.0 and started to use raw strings (e.g. setwd(r"{users\Elchorro\folder}")).
Read more >
Why can't Python's raw string literals end with a single ...
It seems python went with allowing " instead of allowing \ as the last character. However, this shouldn't cause any trouble.
Read more >
RAW STRINGS IN R?! - YouTube
Your browser can ' t play this video. Learn more.
Read more >
How To Use Python Raw String
Raw string is useful when a string needs to contain a backslash, such as for a regular expression or Windows directory path, and...
Read more >
Raw string literal - C# 11.0 draft feature specifications
This feature describes raw string literals. Raw string literals enable string literals to avoid almost all escape sequences.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found