I have strange situation. I create service which work with local stroge and use js file for this. I want to use getAsync fucntion.
localStorage.js
function get(key) {
console.log('getItem')
return localStorage.getItem(key)
}
ILocalStorageService.cs
public async Task<T> GetAsync<T>(string key) where T : class
{
var data = await jsRuntime.InvokeAsync<string>("get", key);
Console.Write("getItem");
if (string.IsNullOrEmpty(data))
{ Console.WriteLine("LocalStorageService: token is null"); return null; }
return JsonSerializer.Deserialize<T>(data);
}
I call this function for get autoriaztion token, which I keep in local storage. This token is object, which have 3 fields. And I call this twice:
First, in autorization on loading page
var token = await localStorageService.GetAsync<Token>(nameof(Token));
Second, when I want to get field value
var operatorToken = await localStorageService.GetAsync<Token>(nameof(Token));
Guid operatorID = Guid.Parse(operatorToken.UserID);
Console.WriteLine("Operator is get");
Token.cs
public class Token
{
public string UserID { get; set; }
public string UserName { get; set; }
public string UserSurname { get; set; }
}
In first I get this token, all work correctly. But in second I just get nothing, no errors, no values. How can you see, I use some logs and in chrome console I see 2 'getItem'. It means I call local storage twice, how I want. But I haven't "Operator is get" in my console. Also I debug this step by step and when I go var data = await jsRuntime.InvokeAsync<string>("get", key);
debuging just end. Why I not get value in second situatiob and how to fix this?