public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
 {
     LogUtility.LogInfo("GetItemExclusive => Session Id: {0}, Session provider object: {1}.", id, this.GetHashCode());
     return(GetItemFromSessionStore(true, context, id, out locked, out lockAge, out lockId, out actions));
 }
        private SessionStateStoreData GetItemFromSessionStore(bool isWriteLockRequired, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            try
            {
                SessionStateStoreData sessionStateStoreData = null;
                locked  = false;
                lockAge = TimeSpan.Zero;
                lockId  = 0;
                actions = SessionStateActions.None;
                if (id == null)
                {
                    return(null);
                }
                GetAccessToStore(id);
                ISessionStateItemCollection sessionData = null;

                int  sessionTimeout;
                bool isLockTaken = false;
                //Take read or write lock and if locking successful than get data in sessionData and also update session timeout
                if (isWriteLockRequired)
                {
                    isLockTaken   = cache.TryTakeWriteLockAndGetData(configuration.IsLocking, DateTime.Now, (int)configuration.RequestTimeout.TotalSeconds, out lockId, out sessionData, out sessionTimeout);
                    sessionId     = id;     // signal that we have to remove lock in EndRequest
                    sessionLockId = lockId; // save lockId for EndRequest
                }
                else
                {
                    isLockTaken = cache.TryCheckWriteLockAndGetData(configuration.IsLocking, out lockId, out sessionData, out sessionTimeout);
                }

                if (isLockTaken)
                {
                    locked = false;
                    LogUtility.LogInfo("GetItemFromSessionStore => Session Id: {0}, Session provider object: {1} => Lock taken with lockId: {2}", id, this.GetHashCode(), lockId);
                }
                else
                {
                    sessionId     = null;
                    sessionLockId = null;
                    locked        = true;
                    LogUtility.LogInfo("GetItemFromSessionStore => Session Id: {0}, Session provider object: {1} => Can not lock, Someone else has lock and lockId is {2}", id, this.GetHashCode(), lockId);
                }

                // If locking is not successful then do not return any result just return lockAge, locked=true and lockId.
                // ASP.NET tries to acquire lock again in 0.5 sec by calling this method again. Using lockAge it finds if
                // lock has been taken more than http request timeout than ASP.NET calls ReleaseItemExclusive and calls this method again to get lock.
                if (locked)
                {
                    lockAge = cache.GetLockAge(lockId);
                    return(null);
                }

                if (sessionData == null)
                {
                    // If session data do not exists means it might be exipred and removed. So return null so that asp.net can call CreateUninitializedItem and start again.
                    // But we just locked the record so first release it
                    ReleaseItemExclusive(context, id, lockId);
                    return(null);
                }

                // Restore action flag from session data
                if (sessionData["SessionStateActions"] != null)
                {
                    actions = (SessionStateActions)Enum.Parse(typeof(SessionStateActions), sessionData["SessionStateActions"].ToString());
                }

                //Get data related to this session from sessionDataDictionary and populate session items
                sessionData.Dirty     = false;
                sessionStateStoreData = new SessionStateStoreData(sessionData, new HttpStaticObjectsCollection(), sessionTimeout);
                return(sessionStateStoreData);
            }
            catch (Exception e)
            {
                LogUtility.LogError("GetItemFromSessionStore => {0}", e.ToString());
                locked        = false;
                lockId        = null;
                lockAge       = TimeSpan.Zero;
                actions       = 0;
                LastException = e;
                if (configuration.ThrowOnError)
                {
                    throw;
                }
                return(null);
            }
        }
 public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
 {
     //Creating empty session store data and return it.
     LogUtility.LogInfo("CreateNewStoreData => Session provider object: {0}.", this.GetHashCode());
     return(new SessionStateStoreData(new ChangeTrackingSessionStateItemCollection(), new HttpStaticObjectsCollection(), timeout));
 }