hanssens / localstorage

LocalStorage for .NET - A simple and lightweight tool for persisting data in dotnet (core) apps.
MIT License
74 stars 17 forks source link

Error with new LocalStorage instance #26

Open ahmedsaidzahran opened 5 years ago

ahmedsaidzahran commented 5 years ago

JsonReaderException: Additional text encountered after finished reading JSON content: c. Path '', line 1, position 2.

using (var storage = new LocalStorage())

it worked fine but when used .Clear() it crashed, also I used the new release v2.0.0-beta-2 but nothing

hanssens commented 5 years ago

The error you mentioned occurred in older versions. However, it should be resolved in v2.0.0-beta-2. I'm really interested in getting to the bottom of this and make sure it's not a regression issue.

@ahmedsaidzahran Do you have additional info, or a code sample, that helps me reproduce the problem?

MuhammadAbdelsalam commented 3 years ago

@hanssens and @ahmedsaidzahran, The issue is related to using(){}. The storage object is not destroyed so it is overrated by the new object that was created in an inner method that responsible for storing the value. you will find below my examples for the issue and my solution.

Issue Ex:

  using (var storage = new LocalStorage())
            {

                if(storage.Exists(Strings.Token))
                {
                    return storage.Get(Strings.Token).ToString();
                }
                else
                {
                    GenerateToken().Wait();
                    return storage.Get(Strings.Token).ToString();

                }
            }

Soultion:

           var storage = new LocalStorage();
            if (storage.Exists(Strings.Token))
            {
                return storage.Get(Strings.Token).ToString();
            }
            else
            {
                GenerateToken().Wait();
                return storage.Get(Strings.Token).ToString();

            }
MuhammadAbdelsalam commented 3 years ago

@hanssens, I suggest we could convert the Storage property to be static.

hanssens commented 3 years ago

I can't reproduce the problem you mention. Is there any way you @ahmedsaidzahran or @MuhammadAbdelsalam can provide me with an example?

Something like for example this Clear() test would be great as a reusable example.