异步Web方法
我有一个客户/服务。
该服务有一个需要很长时间才能完成的方法(它与数据库交互)。
我通过从页面到客户端、然后到服务并返回的 AJAX 请求来调用此方法。
我的服务代码:
[WebMethod]
public static string LookupUPC(string sessionId, string upc) {
string response = "";
var client = new SmartShopService.SmartShopInterfaceClient();
try {
response = client.LookupUPC(sessionId, upc);
}
catch (Exception e) {
throw e;
}
finally {
if (client.State == System.ServiceModel.CommunicationState.Faulted)
client.Abort();
else
client.Close();
}
return response;
}
它是通过 AJAX 请求从页面调用的
for(var i = 0;i<10; i++){
$.ajax({
type: "POST",
url: "SmartShopGUI.aspx/LookupUPC",
contentType: "application/json; charset=utf-8",
data: DataCreator(allData),
dataType: "json",
success: function (result) {
$(upcName).html(result.d);
},
error: AjaxFailed
});
}
现在,这是在页面上异步完成的,但客户端同步发送请求。我想更改它,以便如果它一次请求 10 个请求,它将向服务发送 10 个不同的请求。
http://www.screencast-o-matic.com/watch/cX1Qo8qV2
这是一个可能有帮助的视频。
I have a client/service.
The service has a method that takes a long time to do (it interacts with a database).
I'm call this method via a AJAX request from the page to the client, then to the service and back.
My service code:
[WebMethod]
public static string LookupUPC(string sessionId, string upc) {
string response = "";
var client = new SmartShopService.SmartShopInterfaceClient();
try {
response = client.LookupUPC(sessionId, upc);
}
catch (Exception e) {
throw e;
}
finally {
if (client.State == System.ServiceModel.CommunicationState.Faulted)
client.Abort();
else
client.Close();
}
return response;
}
It is called from the page by an AJAX request
for(var i = 0;i<10; i++){
$.ajax({
type: "POST",
url: "SmartShopGUI.aspx/LookupUPC",
contentType: "application/json; charset=utf-8",
data: DataCreator(allData),
dataType: "json",
success: function (result) {
$(upcName).html(result.d);
},
error: AjaxFailed
});
}
Now, this is done asynchronously on the page, but the client is sending the requests synchronously. I want to change it so that if it asks for 10 all at once, it'll send 10 different requests to the service.
http://www.screencast-o-matic.com/watch/cX1Qo8qV2
Here is a video that might help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
去掉webmethod中对Session的依赖,你可能会发现会话访问是串行的,这就是块的本质。
http://msdn.microsoft.com/en-us/library/ms178581.aspx
Remove the reliance on Session in the webmethod, you'll probably find that session access is serial and that is what the block is.
http://msdn.microsoft.com/en-us/library/ms178581.aspx
您最好创建一个 BatchLookup API,它可以一次性处理一组请求。浏览器和服务器之间的任何连接都将受到可以同时发出的请求数量的限制,并且每次往返本身都是一个缓慢的操作。
我怀疑这根本不是服务器端问题,而是您遇到的浏览器连接限制。即使您修复服务器端以处理两个同时请求,浏览器也不会一次将所有 10 个都开火。例如,请参阅有关以下主题的这篇文章: http://www.ajaxperformance.com/2006/12/18/circumventing-browser-connection-limits-for-fun-and-profit/
顺便说一句,在数据库中查找单个 UPC 代码怎么会花费这么长时间?您是否正确索引了它?
You would do better to create a BatchLookup API that can handle a block of requests all in one go. Any connection between a browser and a server will be limited as to how many simultaneous requests you can make and each round trip is a slow operation itself.
I suspect that this isn't a server-side issue at all but the browser connection limitation that you are hitting. Even if you fix the server-side to handle two simultaneous requests the browser isn't going to fire off all 10 of them at once. See for example this article on the topic: http://www.ajaxperformance.com/2006/12/18/circumventing-browser-connection-limits-for-fun-and-profit/
BTW, how can looking up a single UPC code in a database take so long? Do you have it indexed properly?