React to Session api review changes
This commit is contained in:
parent
6cd277e8aa
commit
0469103683
|
|
@ -100,7 +100,7 @@ namespace MvcSample.Web
|
||||||
#endif
|
#endif
|
||||||
app.UseRequestLocalization();
|
app.UseRequestLocalization();
|
||||||
|
|
||||||
app.UseInMemorySession();
|
app.UseSession();
|
||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
{
|
{
|
||||||
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
|
routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
|
||||||
|
|
|
||||||
|
|
@ -61,11 +61,16 @@ namespace Microsoft.AspNet.Mvc
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tempDataDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
|
||||||
var session = context.Session;
|
var session = context.Session;
|
||||||
|
if (session == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tempDataDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
||||||
byte[] value;
|
byte[] value;
|
||||||
|
|
||||||
if (session != null && session.TryGetValue(TempDataSessionStateKey, out value))
|
if (session.TryGetValue(TempDataSessionStateKey, out value))
|
||||||
{
|
{
|
||||||
using (var memoryStream = new MemoryStream(value))
|
using (var memoryStream = new MemoryStream(value))
|
||||||
using (var writer = new BsonReader(memoryStream))
|
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
|
// 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.
|
// so that it is established before the headers are sent.
|
||||||
session[TempDataSessionStateKey] = new byte[] { };
|
session.Set(TempDataSessionStateKey, new byte[] { });
|
||||||
}
|
}
|
||||||
|
|
||||||
return tempDataDictionary;
|
return tempDataDictionary;
|
||||||
|
|
@ -165,7 +170,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
using (var writer = new BsonWriter(memoryStream))
|
using (var writer = new BsonWriter(memoryStream))
|
||||||
{
|
{
|
||||||
_jsonSerializer.Serialize(writer, values);
|
_jsonSerializer.Serialize(writer, values);
|
||||||
session[TempDataSessionStateKey] = memoryStream.ToArray();
|
session.Set(TempDataSessionStateKey, memoryStream.ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,12 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Http.Features;
|
using Microsoft.AspNet.Http.Features;
|
||||||
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -25,7 +26,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
GetHttpContext(session: null, sessionEnabled: true));
|
GetHttpContext(session: null, sessionEnabled: true));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Empty(tempDataDictionary);
|
Assert.Null(tempDataDictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -36,7 +37,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var tempDataDictionary = testProvider.LoadTempData(
|
var tempDataDictionary = testProvider.LoadTempData(
|
||||||
GetHttpContext(Mock.Of<ISessionCollection>()));
|
GetHttpContext(Mock.Of<ISession>()));
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Empty(tempDataDictionary);
|
Assert.Empty(tempDataDictionary);
|
||||||
|
|
@ -180,7 +181,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "string", "value" }
|
{ "string", "value" }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -200,7 +201,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "int", 10 }
|
{ "int", 10 }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -222,7 +223,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "bool", value }
|
{ "bool", value }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -243,7 +244,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "DateTime", inputDatetime }
|
{ "DateTime", inputDatetime }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -264,7 +265,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "Guid", inputGuid }
|
{ "Guid", inputGuid }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -284,7 +285,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "List`string", new List<string> { "one", "two" } }
|
{ "List`string", new List<string> { "one", "two" } }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -310,7 +311,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "Dictionary", inputDictionary }
|
{ "Dictionary", inputDictionary }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -330,7 +331,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
{ "EmptyDictionary", new Dictionary<string, int>() }
|
{ "EmptyDictionary", new Dictionary<string, int>() }
|
||||||
};
|
};
|
||||||
var context = GetHttpContext(new TestSessionCollection(), true);
|
var context = GetHttpContext(new TestSession(), true);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
testProvider.SaveTempData(context, input);
|
testProvider.SaveTempData(context, input);
|
||||||
|
|
@ -346,43 +347,35 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public int DummyInt { get; set; }
|
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<HttpContext>();
|
var httpContext = new DefaultHttpContext();
|
||||||
if (session != null)
|
if(sessionEnabled)
|
||||||
{
|
{
|
||||||
httpContext.Setup(h => h.Session).Returns(session);
|
httpContext.SetFeature<ISessionFeature>(new SessionFeature() { Session = session });
|
||||||
}
|
}
|
||||||
else if (!sessionEnabled)
|
return httpContext;
|
||||||
{
|
|
||||||
httpContext.Setup(h => h.Session).Throws<InvalidOperationException>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
httpContext.Setup(h => h.Session[It.IsAny<string>()]);
|
|
||||||
}
|
|
||||||
if (sessionEnabled)
|
|
||||||
{
|
|
||||||
httpContext.Setup(h => h.GetFeature<ISessionFeature>()).Returns(Mock.Of<ISessionFeature>());
|
|
||||||
}
|
|
||||||
return httpContext.Object;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestSessionCollection : ISessionCollection
|
private class SessionFeature : ISessionFeature
|
||||||
|
{
|
||||||
|
public ISession Session { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestSession : ISession
|
||||||
{
|
{
|
||||||
private Dictionary<string, byte[]> _innerDictionary = new Dictionary<string, byte[]>();
|
private Dictionary<string, byte[]> _innerDictionary = new Dictionary<string, byte[]>();
|
||||||
|
|
||||||
public byte[] this[string key]
|
public IEnumerable<string> Keys { get { return _innerDictionary.Keys; } }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _innerDictionary[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
set
|
public Task LoadAsync()
|
||||||
{
|
{
|
||||||
_innerDictionary[key] = value;
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task CommitAsync()
|
||||||
|
{
|
||||||
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
|
|
@ -390,17 +383,12 @@ namespace Microsoft.AspNet.Mvc
|
||||||
_innerDictionary.Clear();
|
_innerDictionary.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
|
|
||||||
{
|
|
||||||
return _innerDictionary.GetEnumerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(string key)
|
public void Remove(string key)
|
||||||
{
|
{
|
||||||
_innerDictionary.Remove(key);
|
_innerDictionary.Remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set(string key, ArraySegment<byte> value)
|
public void Set(string key, byte[] value)
|
||||||
{
|
{
|
||||||
_innerDictionary[key] = value.ToArray();
|
_innerDictionary[key] = value.ToArray();
|
||||||
}
|
}
|
||||||
|
|
@ -409,11 +397,6 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
return _innerDictionary.TryGetValue(key, out value);
|
return _innerDictionary.TryGetValue(key, out value);
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
|
||||||
{
|
|
||||||
return _innerDictionary.GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -20,7 +20,7 @@ namespace TempDataWebSite
|
||||||
{
|
{
|
||||||
app.UseCultureReplacer();
|
app.UseCultureReplacer();
|
||||||
|
|
||||||
app.UseInMemorySession();
|
app.UseSession();
|
||||||
app.UseMvcWithDefaultRoute();
|
app.UseMvcWithDefaultRoute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue