public void TestUpdates()
        {
            SessionStateItemCollection items = new SessionStateItemCollection();
            items["Val1"] = "value";
            byte[] serializedItems = Serialize(items);
            Binary b = new Binary(serializedItems);
            List<string> ids = new List<string>();
            ICursor allSessions;
            using (var mongo = new Mongo(config))
            {
                mongo.Connect();
                allSessions = mongo["session_store"]["sessions"].FindAll();
                foreach (Document session in allSessions.Documents)
                {
                    string id = (string)session["SessionId"];
                    ids.Add(id);

                }
            }
            foreach (string s in ids)
            {
                var sessionStore = new SessionStore("test");
                sessionStore.UpdateSession(s, 2, b, "AppName", items.Count, 0);
            }
        }
        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;
     }
 }
        //
        // GetSessionStoreItem is called by both the GetItem and
        // GetItemExclusive methods. GetSessionStoreItem retrieves the
        // session data from the data source. If the lockRecord parameter
        // is true (in the case of GetItemExclusive), then GetSessionStoreItem
        // locks the record and sets a new LockId and LockDate.
        //
        private SessionStateStoreData GetSessionStoreItem(bool lockRecord,
            HttpContext context,
            string id,
            out bool locked,
            out TimeSpan lockAge,
            out object lockId,
            out SessionStateActions actionFlags)
        {
            // Initial values for return value and out parameters.
            SessionStateStoreData item = null;
            lockAge = TimeSpan.Zero;
            lockId = null;
            locked = false;
            actionFlags = 0;

            // byte array to hold serialized SessionStateItemCollection.
            byte[] serializedItems = new byte[0];

            var sessionStore = new SessionStore(_applicationName);
            try
            {
                Session session = sessionStore.Get(id, this._applicationName);
                // lockRecord is true when called from GetItemExclusive and
                // false when called from GetItem.
                // Obtain a lock if possible. Evict the record if it is expired.
                if (session == null)
                {
                    // Not found. The locked value is false.
                    locked = false;
                }
                else if (session.Expires < DateTime.Now)
                {
                    locked = false;
                    sessionStore.EvictSession(session);

                }
                else if (session.Locked)
                {
                    locked = true;
                    lockAge = DateTime.Now.Subtract(session.LockDate);
                    lockId = session.LockID;
                }
                else
                {
                    locked = false;
                    lockId = session.LockID;
                    actionFlags = (SessionStateActions)session.Flags;

                    if (lockRecord)
                    {
                        lockId = (int)lockId + 1;
                        session.LockID = lockId;
                        session.Flags = 0;
                        sessionStore.LockSession(session);
                    }

                    if (actionFlags == SessionStateActions.InitializeItem)
                        item = CreateNewStoreData(context, sessionStateSection.Timeout.Minutes);
                    else
                        item = Deserialize(context, session.SessionItems.Bytes, session.Timeout);
                }

            }
            catch (MongoConnectionException e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetSessionStoreItem");
                    throw new ProviderException(e.Message, e.InnerException);
                }
                else
                    throw e;
            }
            return item;
        }
 public override void ResetItemTimeout(HttpContext context, string id)
 {
     var sessionStore = new SessionStore(_applicationName);
     try
     {
         sessionStore.UpdateSessionExpiration(id, this._applicationName, sessionStateSection.Timeout.TotalMinutes);
     }
     catch (Exception e)
     {
         if (WriteExceptionsToEventLog)
         {
             WriteToEventLog(e, "ResetItemTimeout");
             throw new ProviderException(e.Message, e.InnerException);
         }
         else
             throw e;
     }
 }
 //
 // SessionStateProviderBase.RemoveItem
 //
 public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
 {
     var sessionStore = new SessionStore(this._applicationName);
     try
     {
         sessionStore.EvictSession(id, this._applicationName, lockId);
     }
     catch (Exception e)
     {
         if (WriteExceptionsToEventLog)
         {
             WriteToEventLog(e, "RemoveItem");
             throw new ProviderException(e.Message, e.InnerException);
         }
         else
             throw e;
     }
 }
 //
 // SessionStateProviderBase.ReleaseItemExclusive
 //
 public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
 {
     var sessionStore = new SessionStore(_applicationName);
     try
     {
         sessionStore.ReleaseLock(id, this._applicationName, lockId, sessionStateSection.Timeout.TotalMinutes);
     }
     catch (Exception e)
     {
         if (WriteExceptionsToEventLog)
         {
             WriteToEventLog(e, "ReleaseItemExclusive");
             throw new ProviderException(e.Message, e.InnerException);
         }
         else
             throw e;
     }
 }
        //public string ApplicationName
        //{
        //    get { return _applicationName; }
        //}
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            if (name == null || name.Length == 0)
                name = "MongoSessionStore";

            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "MongoDB Session State Store provider");
            }
            // Initialize the abstract base class.
            base.Initialize(name, config);

            _applicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;

            Configuration cfg = WebConfigurationManager.OpenWebConfiguration(_applicationName);
            sessionStateSection = (SessionStateSection)cfg.GetSection("system.web/sessionState");
            if (config["writeExceptionsToEventLog"] != null)
            {
                if (config["writeExceptionsToEventLog"].ToUpper() == "TRUE")
                    _logExceptions = true;
            }

            sessionStore = new SessionStore(_applicationName);
        }