Esempio n. 1
0
 /// <summary>
 /// 创建未初始化的项,就是初始化Session数据
 /// </summary>
 /// <param name="context"></param>
 /// <param name="id"></param>
 /// <param name="timeout"></param>
 public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
 {
     using (SessionStateEF db = new SessionStateEF())
     {
         var session = new ASPStateTempSessions
         {
             Created   = DateTime.Now,
             Expires   = DateTime.Now.AddMinutes(timeout),
             Flags     = (int)SessionStateActions.InitializeItem,
             LockDate  = DateTime.Now,
             Locked    = false,
             SessionId = id,
             LockId    = 0,
             Timeout   = timeout
         };
         db.ASPStateTempSessions.Add(session);
         db.SaveChanges();
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 新建或者释放锁定的项并设置Session的值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="item"></param>
        /// <param name="lockId"></param>
        /// <param name="newItem"></param>
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            using (SessionStateEF db = new SessionStateEF())
            {
                // 判断是否是新建,如果是新建则和CreateUninitializedItem不同在于Timeout和有初始值。
                if (newItem)
                {
                    var session = new ASPStateTempSessions
                    {
                        Created     = DateTime.Now,
                        Expires     = DateTime.Now.AddMinutes(item.Timeout),
                        Flags       = (int)SessionStateActions.None,
                        LockDate    = DateTime.Now,
                        Locked      = false,
                        SessionId   = id,
                        LockId      = 0,
                        Timeout     = item.Timeout,
                        SessionItem = Serialize((SessionStateItemCollection)item.Items)
                    };
                    db.ASPStateTempSessions.Add(session);
                    db.SaveChanges();
                }
                else// 释放锁定的项并设置Session的值
                {
                    var session = db.ASPStateTempSessions.FirstOrDefault(i => i.SessionId == id);
                    if (session == null)
                    {
                        return;
                    }

                    session.Expires     = DateTime.Now.AddMinutes(item.Timeout);
                    session.Locked      = false;
                    session.LockId      = Convert.ToInt32(lockId);
                    session.SessionItem = Serialize((SessionStateItemCollection)item.Items);
                    db.SaveChanges();
                }
            }
        }