Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Core 2.0 Loading Session asynchronously

$
0
0

Hi,

from the MS docs:

The default session provider in ASP.NET Core loads the session record from the underlying IDistributedCache store asynchronously only if the ISession.LoadAsync method is explicitly called before the TryGetValue, Set, or Remove methods. If LoadAsync is not called first, the underlying session record is loaded synchronously, which could potentially impact the ability of the app to scale.

Since I have the SessionExtensions and set/get only one serializable object throughout my app is it enough to just call the LoadAsync at the beginning of the Get and Set extension methods to have the session loading async?

public static class SessionExtensions
{
    public static void Set<T>(this ISession session, string key, T value)
    {session.LoadAsync();
        session.SetString(key, JsonConvert.SerializeObject(value));
    }

    public static T Get<T>(this ISession session,string key)
    {session.LoadAsync();
        var value = session.GetString(key);
        return value == null ? default(T) :
                              JsonConvert.DeserializeObject<T>(value);
    }
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>