异步Web方法

发布于 2024-11-14 20:18:08 字数 1214 浏览 1 评论 0原文

我有一个客户/服务。

该服务有一个需要很长时间才能完成的方法(它与数据库交互)。

我通过从页面到客户端、然后到服务并返回的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世俗缘 2024-11-21 20:18:08

去掉webmethod中对Session的依赖,你可能会发现会话访问是串行的,这就是块的本质。

http://msdn.microsoft.com/en-us/library/ms178581.aspx

并发请求和会话状态

访问 ASP.NET 会话状态是
每个会话独占,这意味着
如果两个不同的用户
并发请求,访问每个
授予单独会议
同时。然而,如果两个
并发请求是针对
同一会话(通过使用相同的
SessionID值),第一次请求
获得会话的独占访问权限
信息。第二个请求
仅在第一个请求后执行
完成了。 (第二次会议可以
如果独占锁也可以获得访问权限
信息被释放是因为
第一个请求超出了锁
超时。)如果 EnableSessionState
@Page 指令中的值已设置
为只读,请求
只读会话信息不
导致独占锁
会话数据。然而,只读
对会话数据的请求可能仍然
必须等待由a设置的锁
对会话数据的读写请求
清楚。

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

Concurrent Requests and Session State

Access to ASP.NET session state is
exclusive per session, which means
that if two different users make
concurrent requests, access to each
separate session is granted
concurrently. However, if two
concurrent requests are made for the
same session (by using the same
SessionID value), the first request
gets exclusive access to the session
information. The second request
executes only after the first request
is finished. (The second session can
also get access if the exclusive lock
on the information is freed because
the first request exceeds the lock
time-out.) If the EnableSessionState
value in the @ Page directive is set
to ReadOnly, a request for the
read-only session information does not
result in an exclusive lock on the
session data. However, read-only
requests for session data might still
have to wait for a lock set by a
read-write request for session data to
clear.

夕嗳→ 2024-11-21 20:18:08

您最好创建一个 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文