diff --git a/src/Microsoft.AspNet.Session/DistributedSession.cs b/src/Microsoft.AspNet.Session/DistributedSession.cs index 0b0cf9462b..20a9514005 100644 --- a/src/Microsoft.AspNet.Session/DistributedSession.cs +++ b/src/Microsoft.AspNet.Session/DistributedSession.cs @@ -98,10 +98,10 @@ namespace Microsoft.AspNet.Session { if (!_loaded) { - Stream data; - if (_cache.TryGetValue(_sessionId, out data)) + var data = _cache.Get(_sessionId); + if (data != null) { - Deserialize(data); + Deserialize(new MemoryStream(data)); } else if (!_isNewSessionKey) { @@ -115,16 +115,19 @@ namespace Microsoft.AspNet.Session { if (_isModified) { - Stream data; - if (_logger.IsEnabled(LogLevel.Information) && !_cache.TryGetValue(_sessionId, out data)) + var data = _cache.Get(_sessionId); + if (_logger.IsEnabled(LogLevel.Information) && data == null) { _logger.LogInformation("Session {0} started", _sessionId); } _isModified = false; - _cache.Set(_sessionId, context => { - context.SetSlidingExpiration(_idleTimeout); - Serialize(context.Data); - }); + + var stream = new MemoryStream(); + Serialize(stream); + _cache.Set( + _sessionId, + stream.ToArray(), + new DistributedCacheEntryOptions().SetSlidingExpiration(_idleTimeout)); } } @@ -165,9 +168,9 @@ namespace Microsoft.AspNet.Session for (int i = 0; i < expectedEntries; i++) { int keyLength = DeserializeNumFrom2Bytes(content); - var key = new EncodedKey(content.ReadBytes(keyLength)); + var key = new EncodedKey(ReadBytes(content, keyLength)); int dataLength = DeserializeNumFrom4Bytes(content); - _store[key] = content.ReadBytes(dataLength); + _store[key] = ReadBytes(content, dataLength); } } @@ -219,6 +222,22 @@ namespace Microsoft.AspNet.Session return content.ReadByte() << 24 | content.ReadByte() << 16 | content.ReadByte() << 8 | content.ReadByte(); } + private byte[] ReadBytes(Stream stream, int count) + { + var output = new byte[count]; + int total = 0; + while (total < count) + { + var read = stream.Read(output, total, count - total); + if (read == 0) + { + throw new EndOfStreamException(); + } + total += read; + } + return output; + } + // Keys are stored in their utf-8 encoded state. // This saves us from de-serializing and re-serializing every key on every request. private class EncodedKey diff --git a/src/Microsoft.AspNet.Session/project.json b/src/Microsoft.AspNet.Session/project.json index 2eb6c5f09a..105688d976 100644 --- a/src/Microsoft.AspNet.Session/project.json +++ b/src/Microsoft.AspNet.Session/project.json @@ -3,7 +3,7 @@ "description": "ASP.NET 5 session state middleware.", "dependencies": { "Microsoft.AspNet.Http.Extensions": "1.0.0-*", - "Microsoft.Framework.Caching.Distributed": "1.0.0-*", + "Microsoft.Framework.Caching.Memory": "1.0.0-*", "Microsoft.Framework.Logging.Abstractions": "1.0.0-*", "Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" } },