diff --git a/samples/MvcSample.Web/Startup.cs b/samples/MvcSample.Web/Startup.cs index 2a0c067af1..dc2a6feb9f 100644 --- a/samples/MvcSample.Web/Startup.cs +++ b/samples/MvcSample.Web/Startup.cs @@ -100,7 +100,7 @@ namespace MvcSample.Web #endif app.UseRequestLocalization(); - app.UseInMemorySession(); + app.UseSession(); app.UseMvc(routes => { routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}"); diff --git a/src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs b/src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs index 0a809ac835..022131e123 100644 --- a/src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs @@ -61,11 +61,16 @@ namespace Microsoft.AspNet.Mvc return null; } - var tempDataDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); var session = context.Session; + if (session == null) + { + return null; + } + + var tempDataDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); byte[] value; - if (session != null && session.TryGetValue(TempDataSessionStateKey, out value)) + if (session.TryGetValue(TempDataSessionStateKey, out value)) { using (var memoryStream = new MemoryStream(value)) using (var writer = new BsonReader(memoryStream)) @@ -139,7 +144,7 @@ namespace Microsoft.AspNet.Mvc { // Since we call Save() after the response has been sent, we need to initialize an empty session // so that it is established before the headers are sent. - session[TempDataSessionStateKey] = new byte[] { }; + session.Set(TempDataSessionStateKey, new byte[] { }); } return tempDataDictionary; @@ -165,7 +170,7 @@ namespace Microsoft.AspNet.Mvc using (var writer = new BsonWriter(memoryStream)) { _jsonSerializer.Serialize(writer, values); - session[TempDataSessionStateKey] = memoryStream.ToArray(); + session.Set(TempDataSessionStateKey, memoryStream.ToArray()); } } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs index 54a8ef1527..adea9d5826 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs @@ -2,11 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; using Moq; using Xunit; @@ -25,7 +26,7 @@ namespace Microsoft.AspNet.Mvc GetHttpContext(session: null, sessionEnabled: true)); // Assert - Assert.Empty(tempDataDictionary); + Assert.Null(tempDataDictionary); } [Fact] @@ -36,7 +37,7 @@ namespace Microsoft.AspNet.Mvc // Act var tempDataDictionary = testProvider.LoadTempData( - GetHttpContext(Mock.Of())); + GetHttpContext(Mock.Of())); // Assert Assert.Empty(tempDataDictionary); @@ -180,7 +181,7 @@ namespace Microsoft.AspNet.Mvc { { "string", "value" } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -200,7 +201,7 @@ namespace Microsoft.AspNet.Mvc { { "int", 10 } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -222,7 +223,7 @@ namespace Microsoft.AspNet.Mvc { { "bool", value } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -243,7 +244,7 @@ namespace Microsoft.AspNet.Mvc { { "DateTime", inputDatetime } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -264,7 +265,7 @@ namespace Microsoft.AspNet.Mvc { { "Guid", inputGuid } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -284,7 +285,7 @@ namespace Microsoft.AspNet.Mvc { { "List`string", new List { "one", "two" } } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -310,7 +311,7 @@ namespace Microsoft.AspNet.Mvc { { "Dictionary", inputDictionary } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -330,7 +331,7 @@ namespace Microsoft.AspNet.Mvc { { "EmptyDictionary", new Dictionary() } }; - var context = GetHttpContext(new TestSessionCollection(), true); + var context = GetHttpContext(new TestSession(), true); // Act testProvider.SaveTempData(context, input); @@ -346,43 +347,35 @@ namespace Microsoft.AspNet.Mvc public int DummyInt { get; set; } } - private HttpContext GetHttpContext(ISessionCollection session, bool sessionEnabled=true) + private HttpContext GetHttpContext(ISession session, bool sessionEnabled = true) { - var httpContext = new Mock(); - if (session != null) + var httpContext = new DefaultHttpContext(); + if(sessionEnabled) { - httpContext.Setup(h => h.Session).Returns(session); + httpContext.SetFeature(new SessionFeature() { Session = session }); } - else if (!sessionEnabled) - { - httpContext.Setup(h => h.Session).Throws(); - } - else - { - httpContext.Setup(h => h.Session[It.IsAny()]); - } - if (sessionEnabled) - { - httpContext.Setup(h => h.GetFeature()).Returns(Mock.Of()); - } - return httpContext.Object; + return httpContext; } - private class TestSessionCollection : ISessionCollection + private class SessionFeature : ISessionFeature + { + public ISession Session { get; set; } + } + + private class TestSession : ISession { private Dictionary _innerDictionary = new Dictionary(); - public byte[] this[string key] - { - get - { - return _innerDictionary[key]; - } + public IEnumerable Keys { get { return _innerDictionary.Keys; } } - set - { - _innerDictionary[key] = value; - } + public Task LoadAsync() + { + return Task.FromResult(0); + } + + public Task CommitAsync() + { + return Task.FromResult(0); } public void Clear() @@ -390,17 +383,12 @@ namespace Microsoft.AspNet.Mvc _innerDictionary.Clear(); } - public IEnumerator> GetEnumerator() - { - return _innerDictionary.GetEnumerator(); - } - public void Remove(string key) { _innerDictionary.Remove(key); } - public void Set(string key, ArraySegment value) + public void Set(string key, byte[] value) { _innerDictionary[key] = value.ToArray(); } @@ -409,11 +397,6 @@ namespace Microsoft.AspNet.Mvc { return _innerDictionary.TryGetValue(key, out value); } - - IEnumerator IEnumerable.GetEnumerator() - { - return _innerDictionary.GetEnumerator(); - } } } } \ No newline at end of file diff --git a/test/WebSites/TempDataWebSite/Startup.cs b/test/WebSites/TempDataWebSite/Startup.cs index 5decfb7d31..18599bf904 100644 --- a/test/WebSites/TempDataWebSite/Startup.cs +++ b/test/WebSites/TempDataWebSite/Startup.cs @@ -20,7 +20,7 @@ namespace TempDataWebSite { app.UseCultureReplacer(); - app.UseInMemorySession(); + app.UseSession(); app.UseMvcWithDefaultRoute(); } }