Exemplo n.º 1
0
        public Session Get(string id, string applicationName)
        {
            Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName } };
            Session session;
            try
            {

                Document sessionDoc;

                using (var mongo = new Mongo(config))
                {
                    mongo.Connect();
                    sessionDoc = mongo["session_store"]["sessions"].FindOne(selector);
                }

                if (sessionDoc == null)
                {
                    session = null;
                }
                else
                {
                    session = new Session(sessionDoc);
                }

            }
            catch (MongoException ex)
            {
                throw new Exception("Could not insert a new session", ex);
            }

            return session;
        }
Exemplo n.º 2
0
        public void EvictSession(Session session)
        {
            Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName }, { "LockId", session.LockID } };

            using (var mongo = new Mongo(connectionString))
            {
                mongo.Connect();
                mongo[applicationName]["sessions"].Remove(selector);
            }
        }
Exemplo n.º 3
0
        public void EvictSession(Session session)
        {
            var query = Query.And(
                Query.EQ("_id", session.SessionID),
                Query.EQ("ApplicationName", session.ApplicationName),
                Query.EQ("LockID", BsonValue.Create(session.LockID))
            );

            MongoDb.GetCollection(_applicationName).Remove(query);
        }
 public void InsertNewSession()
 {
     var sessionStore = SessionStore.Instance;
     byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
     Binary sessionItems = new Binary(serializedItems);
     string id = Guid.NewGuid().ToString();
     Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
     sessionStore.Insert(session);
     Session storedSession = sessionStore.Get(id, this.ApplicationName);
     Assert.AreEqual(session.SessionID, storedSession.SessionID);
     Assert.AreEqual(session.ApplicationName, storedSession.ApplicationName);
     Assert.AreEqual(session.SessionItems.Bytes.Length, storedSession.SessionItems.Bytes.Length);
 }
 public void UpdateSession()
 {
     var sessionStore = SessionStore.Instance;
     byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
     Binary sessionItems = new Binary(serializedItems);
     string id = Guid.NewGuid().ToString();
     Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
     SessionStore.Instance.Insert(session);
     sessionStore.UpdateSession(id, 5, new Binary(serializedItems), this.ApplicationName, 3, 0);
     Session updatedSession = sessionStore.Get(id, this.ApplicationName);
     Assert.AreEqual(5, updatedSession.Timeout);
     Assert.AreEqual(3, updatedSession.SessionItemsCount);
     Assert.AreEqual(DateTime.Now.AddMinutes(5).Minute, updatedSession.Expires.Minute);
 }
Exemplo n.º 6
0
        public void Insert(Session session)
        {
            Document newSession = new Document() { { "SessionId",session.SessionID }, {"ApplicationName",session.ApplicationName},{"Created",session.Created},
            {"Expires",session.Expires},{"LockDate",session.LockDate},{"LockId",session.LockID},{"Timeout",session.Timeout},{"Locked",session.Locked},
            {"SessionItems",session.SessionItems},{"SessionItemsCount",session.SessionItemsCount},{"Flags",session.Flags}};
            try
            {
                using (var mongo = new Mongo(config))
                {
                    mongo.Connect();
                    mongo["session_store"]["sessions"].Insert(newSession);
                }
            }
            catch (MongoException ex)
            {
                throw new Exception("Could not insert a new session", ex);
            }

        }
        public void TestInserts()
        {
            int i = 0;
            while (i < 200)
            {
                string id = Guid.NewGuid().ToString();
                SessionStateItemCollection items = new SessionStateItemCollection();
                items["Val1"] = "value";
                byte[] serializedItems = Serialize(items);
                Binary b = new Binary(serializedItems);
                Session session = new Session(id, "AppName", 2, b, items.Count, SessionStateActions.None);
                using (var mongo = new Mongo(config))
                {
                    var sessionStore = new SessionStore("test");
                    sessionStore.Insert(session);
                    i++;
                }

            }
        }
 public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
 {
     byte[] serializedItems = new byte[0];
     Binary sessionItems = new Binary(serializedItems);
     Session session = new Session(id, this._applicationName, timeout, sessionItems, 0, SessionStateActions.InitializeItem);
     var sessionStore = new SessionStore(_applicationName);
     try
     {
         sessionStore.Insert(session);
     }
     catch (Exception e)
     {
         if (WriteExceptionsToEventLog)
         {
             WriteToEventLog(e, "CreateUninitializedItem");
             throw new ProviderException(e.Message, e.InnerException);
         }
         else
             throw e;
     }
 }
Exemplo n.º 9
0
        public Session Get(string id, string applicationName)
        {
            Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName } };
            Session session;

            Document sessionDoc;

            using (var mongo = new Mongo(connectionString))
            {
                mongo.Connect();
                sessionDoc = mongo[applicationName]["sessions"].FindOne(selector);
            }

            if (sessionDoc == null)
            {
                session = null;
            }
            else
            {
                session = new Session(sessionDoc);
            }

            return session;
        }
Exemplo n.º 10
0
 public void Insert(Session session)
 {
     MongoDb.GetCollection(_applicationName).Insert(session.ToBsonDocument());
 }
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            try
            {

                byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
                Binary sessionItems = new Binary(serializedItems);

                if (newItem)
                {
                    // Delete an existing expired session if it exists.
                    sessionStore.EvictExpiredSession(id, _applicationName);

                    // insert new session item.
                    Session session = new Session(id, this._applicationName, item.Timeout, sessionItems, item.Items.Count, 0);
                    sessionStore.Insert(session);
                }
                else
                {
                    sessionStore.UpdateSession(id, item.Timeout, sessionItems, this._applicationName, item.Items.Count, lockId);
                }
            }
            catch (MongoConnectionException e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "SetAndReleaseItemExclusive");
                    throw new ProviderException(e.Message, e.InnerException);
                }
                else
                    throw e;
            }
        }
Exemplo n.º 12
0
        public void Insert(Session session)
        {
            Document newSession = new Document() { { "SessionId",session.SessionID }, {"ApplicationName",session.ApplicationName},{"Created",session.Created},
            {"Expires",session.Expires},{"LockDate",session.LockDate},{"LockId",session.LockID},{"Timeout",session.Timeout},{"Locked",session.Locked},
            {"SessionItems",session.SessionItems},{"SessionItemsCount",session.SessionItemsCount},{"Flags",session.Flags}};

            using (var mongo = new Mongo(connectionString))
            {
                mongo.Connect();
                mongo[applicationName]["sessions"].Insert(newSession);
            }
        }
Exemplo n.º 13
0
        public Session Get(string id, string applicationName)
        {
            Session session;

            var query = Query.And(
                Query.EQ("_id", id),
                Query.EQ("ApplicationName", applicationName)
            );

            var sessionDoc = MongoDb.GetCollection(_applicationName).FindOneAs<BsonDocument>(query);

            if (sessionDoc == null)
            {
                session = null;
            }
            else
            {
                session = new Session(sessionDoc);
            }

            return session;
        }
Exemplo n.º 14
0
        public void EvictSession(Session session)
        {
            Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName }, { "LockId", session.LockID } };
            try
            {
                using (var mongo = new Mongo(config))
                {
                    mongo.Connect();
                    mongo["session_store"]["sessions"].Remove(selector);
                }
            }
            catch (MongoException ex)
            {
                throw new Exception("There was a problem when evicting the session with SessionId:" + session.SessionID, ex);
            }

        }
Exemplo n.º 15
0
        public void LockSession(Session session)
        {
            Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName } };
            Document sessionLock = new Document() { { "$set", new Document() {{"LockDate", DateTime.Now },
            {"LockId", session.LockID }, {"Locked", true }, {"Flags",0} } } };

            using (var mongo = new Mongo(connectionString))
            {
                mongo.Connect();
                mongo[applicationName]["sessions"].Update(sessionLock, selector, 0, false);
            }
        }
Exemplo n.º 16
0
 public void LockSessionAndReleaseLock()
 {
     var sessionStore = SessionStore.Instance;
     byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
     Binary sessionItems = new Binary(serializedItems);
     string id = Guid.NewGuid().ToString();
     Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
     SessionStore.Instance.Insert(session);
     DateTime timestamp = DateTime.Now;
     session.LockID = 1;
     sessionStore.LockSession(session);
     Session lockedSesssion = sessionStore.Get(id, this.ApplicationName);
     Assert.AreEqual(true, lockedSesssion.Locked);
     Assert.AreEqual(1,session.LockID);
     Assert.AreNotEqual(session.LockDate,lockedSesssion.LockDate);
     Assert.AreEqual(0, lockedSesssion.Flags);
     sessionStore.ReleaseLock(lockedSesssion.SessionID, lockedSesssion.ApplicationName, lockedSesssion.LockID, item.Timeout);
     Session unlockedSession = sessionStore.Get(id, this.ApplicationName);
     Assert.AreEqual(false, unlockedSession.Locked);
     Assert.AreEqual(lockedSesssion.LockDate, unlockedSession.LockDate);
     Assert.AreNotEqual(lockedSesssion.Expires, unlockedSession.Expires);
 }
Exemplo n.º 17
0
        public void LockSession(Session session)
        {
            Document selector = new Document() { { "SessionId", session.SessionID }, { "ApplicationName", session.ApplicationName } };
            Document sessionLock = new Document() { { "$set", new Document() {{"LockDate", DateTime.Now }, 
            {"LockId", session.LockID }, {"Locked", true }, {"Flags",0} } } };
            try
            {
                using (var mongo = new Mongo(config))
                {
                    mongo.Connect();
                    mongo["session_store"]["sessions"].Update(sessionLock, selector, 0, false);
                }
            }
            catch (MongoException ex)
            {
                throw new Exception("There was a problem when locking the session with SessionId:" + session.SessionID, ex);
            }

        }
Exemplo n.º 18
0
        public void LockSession(Session session)
        {
            var query = Query.And(
                Query.EQ("_id", session.SessionID),
                Query.EQ("ApplicationName", session.ApplicationName)
            );

            var update = Update.Set("LockDate", DateTime.Now)
                               .Set("Locked", true)
                               .Set("LockID", BsonValue.Create(session.LockID))
                               .Set("Flags",0);
            MongoDb.GetCollection(_applicationName).Update(query, update);
        }
Exemplo n.º 19
0
 public void AddExpiredSessionAndEvictSoft()
 {
     var sessionStore = SessionStore.Instance;
     byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
     Binary sessionItems = new Binary(serializedItems);
     string id = Guid.NewGuid().ToString();
     Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
     session.Expires = DateTime.Now.Subtract(new TimeSpan(0,2,0));
     sessionStore.Insert(session);
     sessionStore.EvictExpiredSession(session.SessionID,session.ApplicationName);
     Session storedSession = sessionStore.Get(session.SessionID, session.ApplicationName);
     Assert.IsNull(storedSession);
 }
Exemplo n.º 20
0
 public void InsertNewSessionAndEvictHard()
 {
     var sessionStore = SessionStore.Instance;
     byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
     Binary sessionItems = new Binary(serializedItems);
     string id = Guid.NewGuid().ToString();
     Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
     SessionStore.Instance.Insert(session);
     SessionStore.Instance.EvictSession(session);
     Session storedSession = sessionStore.Get(id, this.ApplicationName);
     Assert.IsNull(storedSession); 
 }