Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

c# - Sessions in Asynchronous design

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...