1

I've created web application that has a sub that builds a contacts list. This sub fetches phone numbers and email address from a contacts db based on ids that are provided by a user. At it's fastest, the application will process about four ids per second. With 200-300 ids at any given time, the completion time is long.

Time is not really the problem, it's end user status updates. I've created a very crude web service that reads the "CurrentRecordNumber" that is stored in a session variable as the app loops through the ids. I intend to use javascript to call the webmethod from the app periodically to update status.

My problem is that when debugging, the webmethod call will complete successfully, but not until the app is finished processing.

This seems like a very simple problem. I must not be using the right terms because my results seem overly complicated.

I'm very new to asynchronous features of ASP.NET so please forgive. I have, however, written some Winforms that incorporate multiple threads so I have a basic understanding of threading.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ian Gainey
  • 15
  • 6
  • 1
    This can help with understanding how async webmethods work in ASP.NET: [Using Asynchronous Methods in ASP.NET MVC 4](http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4). Also, [this](http://stackoverflow.com/a/21372526/1768303) might be related. – noseratio Jan 31 '14 at 22:48
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 01 '14 at 03:20

1 Answers1

0

This is due to the way ASP.NET treats session. You haven't said whether you are using webforms or MVC, but MVC has a quick workaround for this.

First, the problem: SessionState is designed to be accessed by one request at a time, in the order received by the server. Think of this as a queue at the bank with only one bank teller available. The first person in line is the first to be helped (though this is on a per-session basis, not a per-user).

ASP.NET locks all other requests that require SessionState from executing until the previous one is done.

I haven't tried to correct this problem in web forms, but the easiest way I know of would be to not require SessionState on your progress check.

In MVC, there's a SessionState attribute that can be applied to the controller or method, indicating that there's no chance of a call to that method overwriting SessionState. As long as your call is read-only, you can make your controller code use this attribute to allow multiple async requests simultaneously:

<SessionState(SessionStateBehavior.ReadOnly)>
Public Class MyController
ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • Thank you for your response. I'm using web forms. You are correct and it appears that I'm going to have to use another storage area to hold my variable. I really didn't want to make this sql or file-io enabled. I found the following from MS on Session State that sums it up the same way: >However, if two concurrent requests are made for the same session, >the first request gets exclusive access to the session information. >The second request executes only after the first request... – Ian Gainey Feb 03 '14 at 18:53
  • Right. That's the idea behind a queue-- one processes before the next starts. – ps2goat Feb 03 '14 at 19:33