While browsing through the ASP.NET Core documentation I noticed the following section: Load session state asynchronously.
The default session provider in ASP.NET Core loads session records from the underlying IDistributedCache backing store asynchronously only if the ISession.LoadAsync method is explicitly called before the TryGetValue, Set, or Remove methods. If
LoadAsync
isn't called first, the underlying session record is loaded synchronously, which can incur a performance penalty at scale.To have apps enforce this pattern, wrap the DistributedSessionStore and DistributedSession implementations with versions that throw an exception if the
LoadAsync
method isn't called beforeTryGetValue
,Set
, orRemove
. Register the wrapped versions in the services container.
To avoid this performance penalty, I created 2 extensions methods that do a LoadAsync before reading or writing the session state: