2

I have two ajax calls to a web service from jquery.

The first call (GetMessages) starts an interval in javascript (setInterval) and returns a string array of messages stored in a session variable.

The second call (UploadUsers) uploads users and saves the status in the session to be returned in the method GetMessages. So UploadUsers adds messages to the Session and GetMessages retrieves the messages and displays them for the client.

The problem is even though I call both methods asynchronously, GetMessages waits until UploadUsers is finished. It just loads.

I even put a thread.sleep between each user being added and I expect to have GetMessages return "1/10 users have been added", "2/10 users have been added", each on separate calls.

What happens is GetMessages doesn't return anything until UploadUsers is finished and then brings all the text at once.

I have a lot of code so I don't know what to put but here it goes:

UploadUsers.aspx

callAsynchMethod('ClientStatusHandler.asmx/GetMessages',
'', printMessages, stopPollingError);

callAsynchMethod('ClientStatusHandler.asmx/StartRetrievingLeads',
data, stopPolling, stopPollingError);

Site.js

function callAsynchMethod(url, keyValue, callBack, callBackError) {
    $.ajax({
        type: "POST",
        url: url,
        data: keyValue,
        contentType: "application/json; charset=utf-8",
        success: callBack,
        error:callBackError
    });
}

ClientStatusHandler.asmx.cs

const string key = "LUMessages";
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod(EnableSession = true)]
        public string[] GetMessages()
        {

            if (Session[key] != null)
            {
                string[] messages = ((IList<string>)Session[key]).ToArray();
                ((IList<string>)Session[key]).Clear();
                return messages;
            }
            return new string[] { };
        }







  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
     [WebMethod(EnableSession = true)]
      public string[] UploadUsers()
     {
    //This function adds a user and calls SaveMessageToQueue 
//for every user.
    //I see the message being entered into the session and 
//I call Thread.Sleep(10000) 
    //between messages.
    }









     private void SaveMessageToQueue(string m)
        {

        IList<string> messageQueue = (List<string>)HttpContext.Current.
Session["LUMessages"];
        messageQueue.Add(m);
         }
Eitan
  • 1,434
  • 6
  • 21
  • 53

1 Answers1

4

This is because you have enable the Session and the Session lock all the calls until they return.

You can read also this question: Replacing ASP.Net's session entirely

also read: https://stackoverflow.com/a/3660837/159270

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I replaced the code in the GetMessages method to return a hard coded string array to test it and I still get the wait even though he doesn't approach the session. – Eitan Jan 29 '12 at 10:36
  • @Eitan You need to turn EnableSessio to false (or to readonly) – Aristos Jan 29 '12 at 11:09