Permalink
Browse files

Updates

  • Loading branch information...
1 parent 8eade8a commit 25dd71541941d0c9662b223edfdf659bfb2310bf @Sedward Sedward committed Mar 22, 2010
@@ -69,13 +69,14 @@ public override void Dispose()
}
public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)
- {
+ {
return false;
}
public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
{
- string sessionItems = Serialize((SessionStateItemCollection)item.Items);
+ byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
+ Binary sessionItems = new Binary(serializedItems);
try
{
@@ -150,7 +151,7 @@ public override void SetAndReleaseItemExclusive(HttpContext context, string id,
actionFlags = 0;
// String to hold serialized SessionStateItemCollection.
- string serializedItems = "";
+ byte[] serializedItems = new byte[0];
// Timeout value from the data store.
int timeout = 0;
@@ -218,7 +219,7 @@ public override void SetAndReleaseItemExclusive(HttpContext context, string id,
// be stored in an Access Memo field.
//
- private string Serialize(SessionStateItemCollection items)
+ private byte[] Serialize(SessionStateItemCollection items)
{
MemoryStream ms = new MemoryStream();
BinaryWriter writer = new BinaryWriter(ms);
@@ -228,7 +229,7 @@ private string Serialize(SessionStateItemCollection items)
writer.Close();
- return Convert.ToBase64String(ms.ToArray());
+ return ms.ToArray();
}
//
@@ -237,10 +238,10 @@ private string Serialize(SessionStateItemCollection items)
// SessionStateItemCollection.
//
- private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
+ private SessionStateStoreData Deserialize(HttpContext context, byte[] serializedItems, int timeout)
{
MemoryStream ms =
- new MemoryStream(Convert.FromBase64String(serializedItems));
+ new MemoryStream(serializedItems);
SessionStateItemCollection sessionItems =
new SessionStateItemCollection();
@@ -303,7 +304,9 @@ public override void RemoveItem(HttpContext context, string id, object lockId, S
public override void CreateUninitializedItem(HttpContext context,string id,int timeout)
{
- Session session = new Session(id,this._applicationName, timeout, String.Empty, 0, SessionStateActions.InitializeItem);
+ byte[] serializedItems = new byte[0];
+ Binary sessionItems = new Binary(serializedItems);
+ Session session = new Session(id,this._applicationName, timeout, sessionItems, 0, SessionStateActions.InitializeItem);
try
{
@@ -330,7 +333,7 @@ public override void ResetItemTimeout(HttpContext context, string id)
{
try
{
- SessionStore.UpdateSessionExpiration(id, DateTime.Now.AddMinutes(sessionStateSection.Timeout.TotalMinutes));
+ SessionStore.UpdateSessionExpiration(id,this._applicationName, sessionStateSection.Timeout.TotalMinutes);
}
catch (Exception e)
{
@@ -18,7 +18,7 @@ public class Session
private object _lockID;
private int _timeout;
private bool _locked;
- private string _sessionItems;
+ private Binary _sessionItems;
private int _sessionItemsCount;
private int _flags;
@@ -27,7 +27,7 @@ public class Session
public Session() { }
- public Session(string id, string applicationName, int timeout, string sessionItems, int sessionItemsCount,SessionStateActions actionFlags )
+ public Session(string id, string applicationName, int timeout, Binary sessionItems, int sessionItemsCount,SessionStateActions actionFlags )
{
this._sessionID = id;
this._applicationName = applicationName;
@@ -51,7 +51,7 @@ public Session(Document document)
this._lockID = (int)document["LockId"];
this._timeout = (int)document["Timeout"];
this._locked = (bool)document["Locked"];
- this._sessionItems = (string)document["SessionItems"];
+ this._sessionItems = (Binary)document["SessionItems"];
this._sessionItemsCount = (int)document["SessionItemsCount"];
this._flags = (int)document["Flags"];
this._created = (DateTime)document["Created"];
@@ -109,7 +109,7 @@ public bool Locked
set { this._locked = value; }
}
- public string SessionItems
+ public Binary SessionItems
{
get { return this._sessionItems; }
set { this._sessionItems = value; }
@@ -67,15 +67,15 @@ public static Session Get(string id, string applicationName)
return session;
}
- public static void UpdateSession(string id, int timeout, string sessionItems, string applicationName, int sessionItemsCount, object lockId)
+ public static void UpdateSession(string id, int timeout, Binary sessionItems, string applicationName, int sessionItemsCount, object lockId)
{
try
{
- Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName } };
- Document session = new Document() { { "$set", new Document() { { "LockDate", DateTime.Now.AddMinutes((double)timeout) }, { "LockId", lockId }, { "Locked", true } } } };
+ Document selector = new Document() { { "SessionId", id }, { "ApplicationName", applicationName },{"LockId",lockId} };
+ Document session = new Document() { { "$set", new Document() { { "LockDate", DateTime.Now.AddMinutes((double)timeout) },{"Timeout",timeout},{ "SessionItems", sessionItems },{"SessionItemsCount",sessionItemsCount}} } };
conn.Open();
- sessions.Update(session);
+ sessions.Update(session, selector, 0, false);
}
catch (MongoException ex)
{
@@ -5,7 +5,7 @@
</configSections>
<Mongo>
<connections>
- <add key="mongoserver" connectionString="Server=localhost:27018"/>
+ <add key="mongoserver" connectionString="Server=localhost:27017"/>
</connections>
</Mongo>
</configuration>
@@ -50,31 +50,32 @@ public void TearDown()
[Test]
public void InsertNewSession()
- {
-
- try
- {
- string sessionItems = Serialize((SessionStateItemCollection)item.Items);
- OidGenerator oGen = new OidGenerator();
- string id = oGen.Generate().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);
-
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + " " + ex.InnerException.Message);
- }
+ {
+ byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
+ Binary sessionItems = new Binary(serializedItems);
+ OidGenerator oGen = new OidGenerator();
+ string id = oGen.Generate().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);
+ }
- //Session storedSession = SessionStore.Get(id, this.ApplicationName);
- //if (storedSession == null)
- //{
- // Console.WriteLine("It's null");
- //}
- //Assert.AreEqual(session.SessionID, storedSession.SessionID);
- //Assert.AreEqual(session.ApplicationName, storedSession.ApplicationName);
- //Assert.AreEqual(session.Created, storedSession.Created);
+ [Test]
+ public void UpdateSession()
+ {
+ byte[] serializedItems = Serialize((SessionStateItemCollection)item.Items);
+ Binary sessionItems = new Binary(serializedItems);
+ OidGenerator oGen = new OidGenerator();
+ string id = oGen.Generate().ToString();
+ Session session = new Session(id, this.ApplicationName, this.Timeout, sessionItems, item.Items.Count, SessionStateActions.None);
+ SessionStore.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);
}
[Test]
@@ -91,26 +92,27 @@ public void DumpSessions()
expires = expires.ToLocalTime();
string applicationName = (string)session["ApplicationName"];
int sessionItemsCount = (int)session["SessionItemsCount"];
+ int timeout = (int)session["Timeout"];
bool locked = (bool)session["Locked"];
- Console.WriteLine("SessionId:" + id + " | Created:" + created.ToString() + " | Expires:" + expires.ToString() + " | Locked?: " + locked.ToString() + " | Application:" + applicationName + " | Total Items:" + sessionItemsCount.ToString());
+ Console.WriteLine("SessionId:" + id + " | Created:" + created.ToString() + " | Expires:" + expires.ToString() +" |Timeout:"+ timeout.ToString() + " | Locked?: " + locked.ToString() + " | Application:" + applicationName + " | Total Items:" + sessionItemsCount.ToString());
}
conn.Close();
}
- private string Serialize(SessionStateItemCollection items)
+ private byte[] Serialize(SessionStateItemCollection items)
{
MemoryStream ms = new MemoryStream();
BinaryWriter writer = new BinaryWriter(ms);
if (items != null)
items.Serialize(writer);
writer.Close();
- return Convert.ToBase64String(ms.ToArray());
+ return ms.ToArray();
}
- private SessionStateItemCollection Deserialize(string serializedItems, int timeout)
+ private SessionStateItemCollection Deserialize(byte[] serializedItems, int timeout)
{
- MemoryStream ms = new MemoryStream(Convert.FromBase64String(serializedItems));
+ MemoryStream ms = new MemoryStream(serializedItems);
SessionStateItemCollection sessionItems = new SessionStateItemCollection();
if (ms.Length > 0)
@@ -63,7 +63,7 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
- <ProjectReference Include="..\MongoDBSessionStore\MongoSessionStore.csproj">
+ <ProjectReference Include="..\MongoSessionStore\MongoSessionStore.csproj">
<Project>{354D379B-E16C-40DE-B26F-8CCF2EDE94D8}</Project>
<Name>MongoSessionStore</Name>
</ProjectReference>

0 comments on commit 25dd715

Please sign in to comment.