curiosity
curiosity

Reputation: 201

Can I use HttpContext.Application instead of Session to store session specific data?

I need to access user specific data in the controllers of an ASP.NET MVC 2 application. My controller class has 2 controller methods:

[ControllerSessionState(ControllerSessionState.ReadOnly)]
public class CalendarController : BasicController
{
    [Authorize]
    public ActionResult ControllerMethod1()
    {
       //reads session specific data
    }

    [Authorize]
    public ActionResult ControllerMethod1()
    {
       //stores session specific data
    }
}

I want the requests to be parallelized, that is, ControllerMethod1 should be able to run in parallel with ControllerMethod2. For this reason I marked the session as ReadOnly. However, ControllerMethod2 has to write data to the "session". As this is not allowed, I think about storing my data in HttpContext.Application. I could encode the user-id in the name of the object, for example:

 HttpContext.Application["Data" + currentUser.UserId] = <<my_data>>;

Can I do it this way? Does this approach have any disadvantages?

Upvotes: 0

Views: 649

Answers (1)

Cristian Lupascu
Cristian Lupascu

Reputation: 40566

Don't specify ControllerSessionState.ReadOnly for a controller that obviously needs to write in the session store.

Instead, try to address the concurrency issue another way (for example using an appropriate locking mechanism).

Also, think about what are the odds that the same user accesses both methods at exactly the same time. I think these odds are so low that concurrency is not worth thinking about in this scenario.

Upvotes: 1

Related Questions