Are there limits for session variables in ASP.net?

2019-02-25 05:02发布

问题:

I will be populating DataTable and other controls from a complex object.

  1. Where should I store such an object?
  2. At what size does session variables starts affecting the performance of page?

回答1:

Data in the Session object is stored in memory on the server. Thus the storage limit is the memory available to the server. This data is not sent to the client at any stage, unless you explicitly do so. Instead the MVC code sends a cookie to the client browser once you have assigned any value to the Session object. The value of this cookie is then used to uniquely identify the session.

So...

  1. The Session object is designed specifically so that you can store session-specific data on the server, so is a suitable place for you to put session-specific data structures like you describe.
  2. Because the Session object is server-side only, using Session to store the results of computationally expensive operation that is invariant across multiple page refreshes will speed up page loads, since you can use the previous result instead of having to create it again. Unless you blow out the memory limits on the server, you're not going to see any performance degradation.


回答2:

  1. If it's a per-session object, the Session dictionary is a reasonable place to store it
  2. If you are using the InProcess session store, the size of the object never affects page performance (at least until all of the data causes the process to swap). Other session stores may have a slight impact, based on how long it takes to move the data from e.g. SQL to the local process. This will be fast until your object gets really big.