Sessions in Asynchronous design

浪子不回头ぞ 提交于 2019-11-26 16:54:25

问题


We are building a AJAX enabled web application that makes multiple asynchronous requests to the server. Each of these server requests are long running server tasks with each returning back a JSON object to the html page. Each of these calls need read/write access to the Session object.

But the ASP.NET locks the session object when multiple asynchronous tasks are in process, thus blocking the first asynchronous call. So these asynchronous calls never happen in parallel.

PS: The asynchronous calls are PageMethod calls.

Are Sessions are 'not' recommended in the first place when used along-side asynchronous calls. Any other suggestions on designing this asynchronous request model will be highly appreciated.


回答1:


Thanks to everyone that posted answers and comments to my question. I'm summing up my findings and solution so that someone may find this useful.

Everyone that commented is correct about recommending not to use Sessions in asynchronous calls. So, how did I get around it?

  1. Changed PageMethod call into a HttpHandler implementing IReadOnlySessionState. (In my case, the Ajax call just needs 'read' access into the Session object)
  2. The Client-side JQuery makes the Ajax call to the server HTTPHandler
  3. The @Page EnableSessionState directive can be left to be default (Read/Write)

With the above solution, multiple async calls are possible with each call reading into the session object

For more information about making a Jquery call to a HTTP Handler returning a JSON object, here's the link




回答2:


I would actually agree that sessions are not recommended when using asynchronous calls, in fact use of sessions should be minimal in any case. Most of my apps don't use sessionstate directly at all, except what's required by the Membership stuff internally.



来源:https://stackoverflow.com/questions/5118236/sessions-in-asynchronous-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!