private SessionStateStoreData GetSessionStateStoreData(bool exclusive, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
actions = SessionStateActions.None;
lockAge = TimeSpan.Zero;
locked = false;
lockId = null;
var query = Query.And(Query.EQ("applicationVirtualPath", HostingEnvironment.ApplicationVirtualPath), Query.EQ("id", id));
var bsonDocument = this.mongoCollection.FindOneAs<BsonDocument>(query);
if (bsonDocument == null)
{
locked = false;
}
else if (bsonDocument["expires"].AsDateTime <= DateTime.Now)
{
locked = false;
this.mongoCollection.Remove(Query.And(Query.EQ("applicationVirtualPath", HostingEnvironment.ApplicationVirtualPath), Query.EQ("id", id)));
}
else if (bsonDocument["locked"].AsBoolean == true)
{
lockAge = DateTime.Now.Subtract(bsonDocument["lockDate"].AsDateTime);
locked = true;
lockId = bsonDocument["lockId"].AsInt32;
}
else
{
locked = false;
lockId = bsonDocument["lockId"].AsInt32;
actions = (SessionStateActions)bsonDocument["sessionStateActions"].AsInt32;
}
if (exclusive)
{
lockId = (int)lockId + 1;
actions = SessionStateActions.None;
var update = Update.Set("lockDate", DateTime.Now).Set("lockId", (int)lockId).Set("locked", true).Set("sessionStateActions", SessionStateActions.None);
this.mongoCollection.Update(query, update);
}
if (actions == SessionStateActions.InitializeItem)
{
return CreateNewStoreData(context, sessionStateSection.Timeout.Minutes);
}
using (var memoryStream = new MemoryStream(bsonDocument["sessionStateItems"].AsByteArray))
{
var sessionStateItems = new SessionStateItemCollection();
if (memoryStream.Length > 0)
{
var binaryReader = new BinaryReader(memoryStream);
sessionStateItems = SessionStateItemCollection.Deserialize(binaryReader);
}
return new SessionStateStoreData(sessionStateItems, SessionStateUtility.GetSessionStaticObjects(context), bsonDocument["timeout"].AsInt32);
}
}