Created
January 11, 2017 03:43
-
-
Save thdotnet/d5c0b2320e6b90700b85bd0b4876dc64 to your computer and use it in GitHub Desktop.
c# code to read and update session values persisted into Sql Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void ReadShortSessionItem(SqlBytes SessionItemShort) | |
{ | |
using (MemoryStream ms = new MemoryStream(SessionItemShort.Buffer)) | |
{ | |
using (BinaryReader br = new BinaryReader(ms)) | |
{ | |
var sessionTimeout = br.ReadInt32(); | |
bool sessionDataExists = br.ReadBoolean(); | |
bool containsStaticObjects = br.ReadBoolean(); | |
var sessionItems = sessionDataExists ? SessionStateItemCollection.Deserialize(br) : new SessionStateItemCollection(); | |
var temp = new SessionStateStoreData(sessionItems, null, sessionTimeout); | |
} | |
} | |
} | |
private static void ReadLongSessionItem(SqlBytes SessionItemLong, SqlConnection conn) | |
{ | |
using (MemoryStream ms = new MemoryStream(SessionItemLong.Buffer)) | |
{ | |
using (BinaryReader br = new BinaryReader(ms)) | |
{ | |
var sessionTimeout = br.ReadInt32(); | |
bool sessionDataExists = br.ReadBoolean(); | |
bool containsStaticObjects = br.ReadBoolean(); | |
var sessionItems = sessionDataExists ? SessionStateItemCollection.Deserialize(br) : new SessionStateItemCollection(); | |
SessionState test = (SessionState)sessionItems["SessionState"]; | |
SerializeSessionState(sessionTimeout, sessionDataExists, containsStaticObjects, sessionItems, conn); | |
} | |
} | |
} | |
private static void SerializeSessionState(int sessionTimeout, bool sessionDataExists, bool containsStaticObjects, SessionStateItemCollection sessionItems, SqlConnection conn) | |
{ | |
using (MemoryStream ms2 = new MemoryStream()) | |
{ | |
using (var bw = new BinaryWriter(ms2)) | |
{ | |
bw.Write(sessionTimeout); // session timeout time | |
bw.Write(sessionDataExists); // state | |
bw.Write(containsStaticObjects); // (ignored) | |
sessionItems.Serialize(bw); | |
bw.Write((byte)0xff); | |
ms2.Flush(); | |
var state = ms2.ToArray(); | |
var length = state.Length; | |
SqlCommand sqlCommand = conn.CreateCommand(); | |
if (length <= 7000) | |
{ | |
sqlCommand = new SqlCommand("dbo.TempUpdateStateItemShort", conn); | |
sqlCommand.CommandType = CommandType.StoredProcedure; | |
sqlCommand.CommandTimeout = 0; | |
sqlCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NVarChar, 88)); | |
sqlCommand.Parameters.Add(new SqlParameter("@itemShort", SqlDbType.VarBinary, 7000)); | |
sqlCommand.Parameters.Add(new SqlParameter("@timeout", SqlDbType.Int)); | |
sqlCommand.Parameters.Add(new SqlParameter("@lockCookie", SqlDbType.Int)); | |
} | |
else | |
{ | |
sqlCommand = new SqlCommand("dbo.TempUpdateStateItemLong", conn); | |
sqlCommand.CommandType = CommandType.StoredProcedure; | |
sqlCommand.CommandTimeout = 0; | |
sqlCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NVarChar, 88)); | |
sqlCommand.Parameters.Add(new SqlParameter("@itemLong", SqlDbType.Image, 8000)); | |
sqlCommand.Parameters.Add(new SqlParameter("@timeout", SqlDbType.Int)); | |
sqlCommand.Parameters.Add(new SqlParameter("@lockCookie", SqlDbType.Int)); | |
} | |
sqlCommand.Parameters[0].Value = "111bef7b-f7d6-4b5c-baa0-6ca88781a686"; | |
sqlCommand.Parameters[1].Size = length; | |
sqlCommand.Parameters[1].Value = state; | |
sqlCommand.Parameters[2].Value = sessionTimeout; | |
sqlCommand.Parameters[3].Value = 1; | |
try | |
{ | |
sqlCommand.ExecuteNonQuery(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
//Console.WriteLine(BitConverter.ToString(state).Replace("-", "")); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment