Comments (17)
You're right @Eilon the object must be serializable, but what i mean is to introduce something like what @davidfowl suggest or we can do something like this
if(obj.GetType().IsSerializable)
{
...
}
from session.
I don't believe we will be implementing something like this in V1. Putting it in backlog for now.
from session.
I find it strange you picked on decimal
; ushort
seems a much better candidate to pick on. 😛
var price = HttpContext.Session.Get<decimal>("one-time-discounted-price-offered");
Either way, I listed all of the primitive types as I figured the code would be pretty simple.
Personally, I'd much prefer to use Get<Int32>()
over GetInt32()
; better/faster IntelliSense/ReSharper refactoring options.
from session.
The problem with having fully generic type support is that there is no standard way to convert from an object to a completely arbitrary type. The critical thing to keep in mind with session state is that all data must be serializable because session state might (and often is) on a remote server. This means that complex object graphs or "live" objects cannot be used in session state.
If there are some specific types that you think should be added, we can certainly look at those (e.g. bool
).
from session.
We could also look into allowing a generic overload that requires an implementation of an interface, ISerializer to go from T -> byte[]. Somebody can plug in protobuf or bond
from session.
@davidfowl @Eilon If this may helps the developer to use various types such as bool, DataTable and so on, I can start to plug protobuf or bond
from session.
from session.
+1 for @davidfowl 's earlier suggestion. I like the idea of a set of byte[] <-> T
formatters the web dev can add explicitly.
Though TBH, I think you would probably add that system as a different service which takes the ISessionCollection as a dependency. Separation of concerns between session provider and the data formatting, ya know.
from session.
@davidfowl @lodejard i think it will be nice if we can provide getting and setting the primitive data types out of the box and let web devs to implement ISerializer for custom and complex objects
from session.
This is a prototype for ProtobufSessionFormatter
that implements ISessionFormatter
which is suggested here aspnet/HttpAbstractions#286
public class ProtobufSessionFormatter<T> : ISessionFormatter<T>
{
public ProtobufSessionFormatter()
{
if (typeof(T).IsValueType==false)
{
var members = GetPublicMembers();
RuntimeTypeModel.Default.Add(typeof(T), false).Add(members);
}
}
private string[] GetPublicMembers()
{
return typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(m=>m.MemberType==MemberTypes.Field||m.MemberType==MemberTypes.Property).Select(m => m.Name).ToArray();
}
public T Deserialize(byte[] value)
{
T obj;
using (var ms = new MemoryStream(value))
{
obj= Serializer.Deserialize<T>(ms);
}
return obj;
}
public byte[] Serialize(T value)
{
byte[] data;
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, value);
data = ms.ToArray();
}
return data;
}
}
from session.
Thanks @muratg, hope to be in consideration in the future
from session.
Understandable that you wouldn't implement everything in V1 but how about support for the "easy" ones?
// Not valid syntax, just conveying a list of types to support
public T Get<T>(string key) where T :
byte,
sbyte,
int,
uint,
short,
ushort,
long,
ulong,
float,
double,
char,
bool,
decimal,
System.Guid,
string {
// basically struct, without the ability to have nested reference types (like an actual struct type)
// ...
}
public T Get<T>(string key) where T : struct, IConvertible {
if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enum");
// ...
}
Int32
& String
alone is pretty limited. 😕
from session.
We're not necessarily opposed to adding at least some of these, but I doubt a lot of apps store a decimal
as a standalone value in session state 😄
from session.
Well an (serializable) object storing in session would be nice. (and also bools etc)
from session.
@muratg is the closing mean that this will not support?!! It was in the backlog
from session.
Bringing back for re-triage.
from session.
This issue was moved to dotnet/aspnetcore#2407
from session.
Related Issues (20)
- Remove obsolete options APIs HOT 3
- Update session to configure DistributedMemoryCache options by default HOT 3
- Add logging scope for session HOT 2
- Random "Accessing expired session" warning HOT 4
- Error closing the session with .net 2.1.0-preview1 HOT 12
- Any plans for maximum session size property? HOT 2
- [1.1.4] Flaky test: SessionTests.SessionLogsCacheRefreshException HOT 6
- System.OperationCanceledException at Microsoft.Extensions.Caching.Redis.RedisCache+<RefreshAsync>d__25.MoveNext HOT 4
- cant access Session in other API HOT 5
- Error closing the session HOT 1
- [Patch 2.0.x] Downgrade cancellation logs HOT 1
- Add EventSource/EventCounter tracing and metrics for Session HOT 1
- Session does not work with Redis when the client has HTTP keep-alives disabled HOT 6
- Session cannot be re-loaded HOT 8
- Determine if the session cookie is considered essential HOT 1
- THIS ISSUE TRACKER IS CLOSED - use the Home repo issue tracker
- Session OnStart and Session OnEnd HOT 8
- How to get number of session HOT 3
- Re-initialize Session middleware from controller HOT 2
- How to add a custom session in .net core? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from session.