Asynchronous Web Methods

I have a client / service.

The service has a method that takes a lot of time (it interacts with the database).

I call this method with an 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 at the request of 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 }); } 

This now runs asynchronously on the page, but the client sends requests synchronously. I want to change it so that if it requests all 10 at the same time, it will send 10 different requests to the service.

http://www.screencast-o-matic.com/watch/cX1Qo8qV2

Here is a video that might help.

+4
source share
2 answers

Remove the Session dependency in the web method, you will probably find that the access to the session is sequential, and that is what the block is.

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

Concurrent Queries and Session State

Access to the ASP.NET session state is exclusive per session, which means that if two different users have simultaneous requests, access to each is granted to a separate session at the same time. However, if two simultaneous requests are from the same session (using the same SessionID), the first request gets exclusive access to the Information session. The second request is executed only after the first request is completed. (The second session can also be accessed if the exclusive lock on information is released because the first request exceeds the timeout lock.) If the EnableSessionState value is in the @Page directive on ReadOnly, requesting read-only session information does not result in an exclusive data lock session. However, read-only requests for session data may have to wait for a lock; the set read and write request for session data is clear.

+2
source

You better create a BatchLookup API that can process a block of requests at a time. Any connection between the browser and the server will be limited in terms of how many simultaneous requests you can make, and each round trip is the slowest operation.

I suspect that this is not a server-side issue, but a limitation of the browser connection that you click on. Even if you fix the server side to handle two simultaneous requests, the browser is not going to shoot all 10 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/

By the way, how can I search for one UPC code in a database for so long? Did you index it correctly?

+1
source

All Articles