3

We have been developing asp.net mvc application. we have to maintain some user-specific information as long as user is logged in. What is the option for me? Cache is shared across all users of the site. Session seems to be the option but I have attached webforms for showing Crystal Reports and the Session object used by webforms and MVC is different: Mvc uses HttpSessionStateBase and webforms use HttpSessionState.

How much information should I keep in the session (I have an array of around 30 integers/user)? Should I keep it in the session or some combination of session and cache should be used instead?

Edit: I have created a SessionService that accepts HttpSessionStateBase as Parameter and from Mvc I'm calling it like:

SessionService _service = new SessionService(HttpContext.Session);

it returns HttpSessionStateBase but as Darin suggested it is abstract class so it might as well be using SessionStateWrapper implementation. In the web forms scenario I'm using something like

HttpSessionStateWrapper Session = new HttpSessionStateWrapper(HttpContext.Current.Session);
            SessionService _SessionRepository = new SessionService(Session);

3 Answers 3

9

Storing 30 integers per user in the session is fine.

But reality is, using cache you can get to problems that you need to be aware:

  • Session state - unless using state server or sql server - will be volatile and can be lost by recycling. Also if you move your site to multiple servers, you have to fill the session on all of them if you use volatile session.
  • Many ASP NET feature such as authentication are cookie based and not session based. You need to be aware that they do not work in tandem. For example, if you use forms authentication and restart the site or recycle the application, user stays authenticated while you have lost all your session information.
Sign up to request clarification or add additional context in comments.

2 Comments

+1 nice short. i want to keep info in session so that i don't have to calculate them on each page however if i find nothing in session i m gonna fetch it from db, use it and put it in session for future use.
That's what I meant! Not to assume it is there. 30 integers per user is nothing. You can store a few KB (perhaps up to 50-100KB) per user even in a huge site.
4

Session seems to be the option but i have attached webforms for showing crystal reports and the Session object used by webforms and MVC is different: Mvc uses HttpSessionStateBase and webforms use HttpSessionState.

HttpSessionStateBase is an abstract class and it's implementation is simply a wrapper of the original HttpSessionState object. So as long as you stay within the same application the session is exactly the same between MVC and WebForms and you would be able to share data using it. So in your scenario session seems a correct approach for persisting data.

3 Comments

@Daring one query! if all the cache variables are created in one actionresult then can outputcache be an alternative?.
@Muhammad Adeel Zahid, your approach is correct. Wrapping the session in a HttpSessionStateWrapper when inside the WebForms scenario is correct.
@Tassadaque, no, here we are talking about user specific information. The cache is shared among all users.
0

Nothing wrong with using session for this. Will be fine.

1 Comment

This appears to be a comment, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.