【发布时间】:2012-02-21 13:40:53
【问题描述】:
我有两个来自 jquery 的 Web 服务的 ajax 调用。
第一次调用 (GetMessages) 在 javascript (setInterval) 中启动一个间隔,并返回存储在会话变量中的消息字符串数组。
第二次调用 (UploadUsers) 上传用户并保存会话中的状态以在方法 GetMessages 中返回。因此 UploadUsers 将消息添加到 Session 中,GetMessages 检索消息并将它们显示给客户端。
问题是即使我异步调用这两个方法,GetMessages 也会等到UploadUsers 完成。它只是加载。
我什至在添加的每个用户之间放置了一个 thread.sleep,我希望 GetMessages 返回“1/10 用户已添加”、“2/10 用户已添加”,每个用户都在单独的调用中。
发生的情况是 GetMessages 不会返回任何内容,直到 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);
}
【问题讨论】:
标签: c# jquery asp.net ajax web-services