简体   繁体   中英

serializing session object in json string

I built a strongly typed session object that looks like this:

class MySession
{
  public string UsedID {get;set;}
  public List<int> ListOfInt {get;set;}
  public List<string ListOfString {get;set;}
  .....
}

I'm currently using InProc session so when a page loads, I write:

MySession TheSession = Session["UserSession"] as MySession;

and then later in the code I can access each property with TheSession.XYZ syntax.

This is really cool but I'm thinking that it might be better to store the session in the DB.

I'm thinking about serializing the MySession object in a json string and store the string in a DB that I can retrieve and deserialize when a page loads.

Is this a good way to do it?

Thanks for your suggestions.

ASP.NET Session State Server vs. InProc Session covers the pros and cons of using a state server or in-process session storage.

As for using JSON, it's certainly more lightweight than XML and just about as flexible - at least for your purposes. I see no reason not to go with it.

First of all make sure you need all the data in Session. I sense that you rather need to create database tables and store data in the tables.

If you are sure that you need Session, then it would make sense to use standard <sessionState mode="SQLServer"> . You can read more about Session-State Modes here .

If you are sure you want custom serialization, then it would make sense to use binary serialization instead of JSON. It will need less memory to store and less resources to serialize/deserialize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM