我有两个从jquery到Web服务的ajax调用。
第一次调用(GetMessages)在javascript ()中开始一个间隔,setInterval并返回存储在会话变量中的消息的字符串数组。
GetMessages
setInterval
第二个调用(UploadUsers)上传用户并将状态保存在要返回的会话中GetMessages。因此,UploadUsers将消息添加到Session,而GetMessages检索消息并将其显示给客户端。
UploadUsers
问题是即使我异步调用这两个方法,也要GetMessages等到UploadUsers完成。它只是加载。
我什至在要添加的每个用户之间放置一个thread.sleep,并且我希望GetMessages返回“分别添加1/10个用户”,“添加2/10个用户”,每个返回单独的调用。
发生的事情是GetMessages直到UploadUsers完成才返回任何内容,然后立即带走所有文本。
我有很多代码,所以我不知道要放什么,但是事情就这样了:
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); }
这是因为您已启用会话,并且会话锁定了所有呼叫,直到它们返回。