// // Serialize is called by the SetAndReleaseItemExclusive method to // convert the SessionStateItemCollection into a Binary. // private byte[] Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); try { if (items != null) { items.Serialize(writer); } } catch (Exception e) { if (WriteExceptionsToEventLog) { WriteToEventLog(e, "GetSessionStoreItem"); throw new ProviderException(e.Message, e.InnerException); } else { throw e; } } finally { writer.Close(); } return(ms.ToArray()); }
private string Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); List <string> itemsABorrar = new List <string>(); foreach (string item in items.Keys) { if (!(items[item].GetType().IsSerializable)) { itemsABorrar.Add(item); } } foreach (string item in itemsABorrar) { items.Remove(item); } if (items != null) { items.Serialize(writer); } writer.Close(); return(Convert.ToBase64String(ms.ToArray())); }
protected virtual byte[] SerializeSessionState(SessionStateItemCollection sessionStateItems) { using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { sessionStateItems.Serialize(bw); return(ms.ToArray()); } }
private byte[] Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); if (items != null) { items.Serialize(writer); } writer.Close(); return(ms.ToArray()); }
// // Serialize is called by the SetAndReleaseItemExclusive method to // convert the SessionStateItemCollection into a Base64 string to // be stored in an Access Memo field. // private string 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())); }
/// <summary> /// 序列化Session的数据 /// </summary> /// <param name="items"></param> /// <returns></returns> public string Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); if (items != null) { items.Serialize(writer); } writer.Close(); return(System.Text.Encoding.ASCII.GetString(ms.ToArray())); }
private string Serialize(SessionStateItemCollection items) { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) { if (items != null) { items.Serialize(writer); } } var encryptedData = MachineKey.Protect(ms.ToArray(), "Session Data"); return(Convert.ToBase64String(encryptedData)); } }
/// <summary> /// Serialize is called by the SetAndReleaseItemExclusive method to /// convert the SessionStateItemCollection into a Base64 String private Byte[] Serialize(SessionStateItemCollection items) { Byte[] buffer = new Byte[] { }; using (MemoryStream ms = new MemoryStream()) { BinaryWriter writer = new BinaryWriter(ms); if (items != null) { items.Serialize(writer); } writer.Close(); buffer = ms.ToArray(); } return(buffer); //return Convert.ToBase64String(ms.ToArray()); }
public void writeExternal(java.io.ObjectOutput output) { lock (this) { output.writeBoolean(_needSessionPersistence); if (!_needSessionPersistence) { //indicates that there is nothing to serialize for this object return; } ObjectOutputStream ms = new ObjectOutputStream(output); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms); _items.Serialize(bw); _staticObjects.Serialize(bw); } }
/// <summary> /// 序列化为二进制数据 /// </summary> /// <param name="items">选项</param> /// <returns></returns> public static byte[] Serialize(SessionStateItemCollection items) { if (items == null) { return(null); } using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms)) { items.Serialize(writer); writer.Close(); return(ms.ToArray()); } } }
/// <summary> /// Convert a SessionStateItemCollection into a Base64 string /// </summary> private static string Serialize(SessionStateItemCollection items) { if (items == null || items.Count < 1) { return(string.Empty); } using (MemoryStream mStream = new MemoryStream()) { using (BinaryWriter bWriter = new BinaryWriter(mStream)) { items.Serialize(bWriter); bWriter.Close(); } return(Convert.ToBase64String(mStream.ToArray())); } }
private string Serialize(SessionStateItemCollection items) { using (var stream = new MemoryStream()) { using (var writer = new BinaryWriter(stream)) { if (items != null) { items.Serialize(writer); } writer.Flush(); writer.Close(); return(Convert.ToBase64String(stream.ToArray())); } } }
///<summary> /// Serialize is called by the SetAndReleaseItemExclusive method to /// convert the SessionStateItemCollection into a byte array to /// be stored in the blob field. /// </summary> private string Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); if (items != null) { BinaryWriter writer = new BinaryWriter(ms); items.Serialize(writer); writer.Close(); ms.Close(); byte[] buf = ms.ToArray(); string result = Convert.ToBase64String(buf, 0, buf.Length); return(result); } else { return(""); } }
} // End of the GetSessionStoreItem method #endregion #region Serialization /// <summary> /// Serialize session items /// </summary> private string Serialize(SessionStateItemCollection items) { // Create the string to return string sessionItems = ""; // Create variables MemoryStream stream = null; BinaryWriter writer = null; try { // Create the binary writer stream = new MemoryStream(); writer = new BinaryWriter(stream); // Serialize session items if (items != null) items.Serialize(writer); // Get the serialized string sessionItems = Convert.ToBase64String(stream.ToArray()); } catch(Exception e) { // We do not want to throw an exception string exMessage = e.Message; } finally { // Close the binary writer if(writer != null) { writer.Close(); } } // Return the string return sessionItems; } // End of the Serialize method
/// <summary> /// Session 정보를 직렬화합니다. (일반적인 직렬화 방식은 안되고, SessionStateItemCollection 자체의 Serialize를 사용해서 직렬화합니다.) /// </summary> /// <param name="items"></param> /// <returns></returns> public static byte[] SerializeSessionState(SessionStateItemCollection items) { if (IsDebugEnabled) { log.Debug("세션 정보를 직렬화를 수행합니다..."); } if (items == null) { return(new byte[0]); } using (var ms = new MemoryStream()) using (var writer = new BinaryWriter(ms)) { items.Serialize(writer); writer.Close(); return(ms.ToArray()); } }
/// <summary> /// Saves the session store data into Couchbase /// </summary> /// <param name="bucket">Couchbase bucket to save to</param> /// <param name="id">Session ID</param> /// <param name="useCas">True to use a check and set, false to simply store it</param> /// <returns>True if the value was saved, false if not</returns> public bool SaveData( IBucket bucket, string id, bool useCas) { var ts = TimeSpan.FromMinutes(Timeout); using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { Data.Serialize(bw); // Attempt to save the data and fail if the CAS fails var retval = useCas ? bucket.Upsert(_dataPrefix + id, ms.ToArray(), DataCas, ts) : bucket.Upsert(_dataPrefix + id, ms.ToArray(), ts); return(retval.Success); } }
/// <summary> /// Saves the session store into Couchbase /// </summary> /// <param name="bucket">Couchbase bucket to save to</param> /// <param name="id">Session ID</param> /// <param name="metaOnly">True to only save the meta data</param> /// <param name="useCas">True to use a check and set, false to simply store it</param> /// <returns>True if the value was saved, false if not</returns> public bool Save( IBucket bucket, string id, bool metaOnly, bool useCas) { using (var ms = new MemoryStream()) { // Write the header first WriteHeader(ms); var ts = TimeSpan.FromMinutes(Timeout); // Attempt to write the header and fail if the CAS fails var retval = useCas ? bucket.Upsert(_headerPrefix + id, ms.ToArray(), HeadCas, ts) : bucket.Upsert(_headerPrefix + id, ms.ToArray(), ts); if (!retval.Success) { return(false); } // Now save the data if (!metaOnly) { // Serialize the data ms.Position = 0; using (var bw = new BinaryWriter(ms)) { Data.Serialize(bw); // Attempt to save the data and fail if the CAS fails retval = useCas ? bucket.Upsert(_dataPrefix + id, ms.ToArray(), DataCas, ts) : bucket.Upsert(_dataPrefix + id, ms.ToArray(), ts); } } // Return the success of the operation return(retval.Success); } }
private byte[] Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); try { if (items != null) { items.Serialize(writer); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { writer.Close(); } return(ms.ToArray()); }
public bool Save(IMemcachedClient client, string id, bool metaOnly, bool useCas) { using (var ms = new MemoryStream()) { // Save the header first SaveHeader(ms); var ts = TimeSpan.FromMinutes(Timeout); // Attempt to save the header and fail if the CAS fails bool retval = useCas ? client.Cas(StoreMode.Set, HeaderPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts, HeadCas).Result : client.Store(StoreMode.Set, HeaderPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts); if (retval == false) { return(false); } // Save the data if (!metaOnly) { ms.Position = 0; // Serialize the data using (var bw = new BinaryWriter(ms)) { Data.Serialize(bw); // Attempt to save the data and fail if the CAS fails retval = useCas ? client.Cas(StoreMode.Set, DataPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts, DataCas).Result : client.Store(StoreMode.Set, DataPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts); } } // Return the success of the operation return(retval); } }
public bool Save(IMemcachedClient client, string id, bool metaOnly, bool useCas, ICompressor compressor, Logger logger) { var ts = TimeSpan.FromMinutes(Timeout); bool retval; using (var ms = new MemoryStream()) { // Save the header first SaveHeader(ms); // Attempt to save the header and fail if the CAS fails retval = useCas ? client.Cas(StoreMode.Set, HeaderPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts, HeadCas).Result : client.Store(StoreMode.Set, HeaderPrefix + id, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length), ts); if (retval == false) { return(false); } } // Save the data if (!metaOnly) { byte[] data; ArraySegment <byte> arraySegment; using (var ms = new MemoryStream()) { using (var bw = new BinaryWriter(ms)) { // Serialize the data Data.Serialize(bw); data = ms.ToArray(); } } if (compressor == null) { if (logger != null) { logger.Info(string.Format("Save Item with id '{0}' with size {1}", id, data.LongLength)); } arraySegment = new ArraySegment <byte>(data); } else { var tempdata = compressor.Compress(data); if (logger != null) { logger.Info(string.Format("Save Item with id '{0}' that was compressed from {1} bytes to {2} bytes", id, data.LongLength, tempdata.LongLength)); } arraySegment = new ArraySegment <byte>(tempdata); } // Attempt to save the data and fail if the CAS fails retval = useCas ? client.Cas(StoreMode.Set, DataPrefix + id, arraySegment, ts, DataCas).Result : client.Store(StoreMode.Set, DataPrefix + id, arraySegment, ts); } // Return the success of the operation return(retval); }
/// <summary> /// Converts the session's content into a byte array so we can store it in Memcached. /// </summary> /// <param name="items">The session's items that should be stored in Memcached.</param> /// <returns>A byte array that can be saved in Memcached.</returns> private static byte[] Serialize(SessionStateItemCollection items) { MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); if (items != null) items.Serialize(writer); writer.Close(); return ms.ToArray(); }