0

I want to share data between two async requests to the server.

When some session variable value is changed in the first request (the value is changed continuously) I want the updated values in the second request.

How i should do this?

bzlm
  • 9,626
  • 6
  • 65
  • 92
munish
  • 1
  • 1

2 Answers2

1

If the requests are part of the same session, just use the session on the HttpContext. If not, then store the variable in the application context and update/use it from there.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Tvanfosson if i store the value in session and initialize the session in the first request and then change it as value changes then the second request only gets the value of the session assigned first – munish Aug 24 '10 at 11:50
  • @munish - It could be that the second request is starting before the first request actually changes the value. Try putting the session access inside a critical section (using `lock` on a per-session lock object) so that you are sure that if the first request gets to the critical section first, the second won't get access until the first completes the update. Also, make sure they are using the same session. If not authenticated, you may need to set a cookie to keep the session sticky. – tvanfosson Aug 24 '10 at 12:06
  • Tvanfosson, My actual requirement is like this In mvc.net i am making first async request to server and it takes so much time thats why i want to add some progress bar on the page. For this i am making second asyn request to get the changed value so that i can change the value of progress bar. Thats why i have added session variable and changing the value time to time as save process proceeds. And i want to get this changed value using my second async request. – munish Aug 24 '10 at 12:13
  • 1
    @munish - is it possible that the browser (or server) is returning a cached result for the request? Try adding a timestamp parameter to the request so that it is different every time and make sure you have caching turned off on the server action. – tvanfosson Aug 24 '10 at 12:33
0

As tvanfosson suggested, it is likely that the browser is caching the results, so you only see the first results over and over, especially if you are using HTTP GET instead of POST. To avoid this, apply this attribute the "progress bar" action:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

Also, unless the "progress bar" action executes on a controller that specifically has read-only access to Session, then it will be forced to wait until the Async action has completed. See Andy's answer to "ASP.NET MVC and Ajax, concurrent requests?". This means that you need to split the progress bar action out to a separate controller that has this attribute applied:

[SessionState(SessionStateBehavior.ReadOnly)]

In summary, your progress bar action will need to look like:

[SessionState(SessionStateBehavior.ReadOnly)]
public class ProgressBarController : Controller
{
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult ProgressBar()
    {
        var progress = Session["progress"];
        return Json(progress, JsonRequestBehavior.AllowGet);
    }
}
Community
  • 1
  • 1
Daniel Schilling
  • 4,829
  • 28
  • 60