public static byte[] Write(this SessionStateValue stateValue, bool compressed)
 {
     return(compressed
         ? GenericSerializationUtil.BinaryWriterOperationToByteBufferWithCompression(stateValue,
                                                                                     SerializeSessionState)
         : GenericSerializationUtil.BinaryWriterOperationToByteBuffer(stateValue, SerializeSessionState));
 }
Exemplo n.º 2
0
        public async Task WriteContents(HttpContextBase context, string sessionId, SessionStateValue stateValue,
                                        bool isNew)
        {
            DeregisterLifetimeExtension(context);
            var now = DateTime.UtcNow;

            var compressionSw = Stopwatch.StartNew();
            var payload       = stateValue.Write(_compressionEnabled);

            compressionSw.Stop();
            Trace.TraceEvent(TraceEventType.Verbose, 4,
                             $"{now.ToString("o")} WriteContents: Serialize. Size: {payload.Length}, Elapsed: {compressionSw.Elapsed.TotalSeconds.ToString()}");

            var response = await _container.UpsertItemAsync(new SessionStateRecord
            {
                SessionId   = sessionId,
                CreatedDate = now,
                TtlSeconds  = stateValue.Timeout * 60,
                IsNew       = isNew ? "yes" : null,
                Payload     = payload,
                Compressed  = _compressionEnabled,
            }, new PartitionKey(sessionId),
                                                            new ItemRequestOptions
            {
                ConsistencyLevel             = _consistencyLevel,
                EnableContentResponseOnWrite = false,
            });

            TraceRequestCharge(response, $"WriteContents: UpsertItemAsync. isNew: {isNew}");
        }
        public static SessionStateValue ExtractDataForStorage(this SessionStateStoreData item)
        {
            var items = item.Items.Count > 0 ? item.Items : null;
            var staticObjects = item.StaticObjects.NeverAccessed ? null : item.StaticObjects;

            if (item.Items.Count > 0)
            {
                items = item.Items;
            }

            if (!item.StaticObjects.NeverAccessed)
            {
                staticObjects = item.StaticObjects;
            }

            var state = new SessionStateValue((SessionStateItemCollection) items, staticObjects, item.Timeout);
            return state;
        }
        private static void SerializeSessionState(BinaryWriter writer, SessionStateValue s)
        {
            writer.Write(s.Timeout);
            var hasSessionItems = s.SessionItems != null && s.SessionItems.Count != 0;

            writer.Write(hasSessionItems);
            var hasStaticObjects = s.StaticObjects != null && s.StaticObjects.Count != 0;

            writer.Write(hasStaticObjects);
            if (hasSessionItems)
            {
                s.SessionItems.Serialize(writer);
            }

            if (hasStaticObjects)
            {
                s.StaticObjects.Serialize(writer);
            }
        }
Exemplo n.º 5
0
        public void SerializeDeserializeSessionValue(bool compressed)
        {
            var expected = $"expected_{Guid.NewGuid():N}";
            var sessionStateItemCollection = new SessionStateItemCollection
            {
                ["actual"] = expected
            };

            var serialized = new SessionStateValue(
                sessionStateItemCollection,
                null,
                1)
                             .Write(compressed);

            var deserialized = serialized.ReadSessionState(compressed);

            var actual = deserialized.SessionItems["actual"];

            Assert.AreEqual(expected, actual);
        }