2

I'm working on my local development environment using angular to issue ajax calls to Web.API (IIS 8.5 hosted). I'm making 5 calls via different angular controllers simultaneously, each of which look like this:

$scope.medications = API.Client.MedicationProfiles.query({ clientId: clientId });

The API service has this definition for the Client.MedicationProfiles resource:

this.Client.MedicationProfiles = $resource(
    '/Noteable.API/api/Client/:clientId/MedicationProfiles/:id',
    {
        clientId: '@clientId',
        id: '@Id'
    },
    {
        update: { method: 'PUT' }
    }
);

I can see all of the requests going to the server basically at once using the chrome dev tools as the page loads but it appeared that they were coming back one at a time as if they had been run synchronously. I put a thread.sleep in one of the Web.API controller actions and sure enough, all calls issued after that one fail to return until the sleeping one does. This doesn't make any sense to me and I'm wondering if someone can tell me in what case they would expect this.

scniro
  • 16,844
  • 8
  • 62
  • 106
sonicblis
  • 2,926
  • 7
  • 30
  • 48

2 Answers2

3

This may be a good situation to implement an asynchronous pattern on your web api backend - you'll be alleviated from your ajax calls blocking once they hit your controller. This can be achieved by leveraging an async and await pattern. You'll want to return a Task, which represents an asynchronous operation.

An example may include...

public async Task<IHttpActionResult> Put(string clientId)
{
    var response = await this.yourService.yourSavingMethod(clientId);

    return Ok(response);
}

Also see SO question Why is this web api controller not concurrent? for more details and insight on the issue. Furthermore, the default behavior for ASP.NET Session State and can be changed to accommodate concurrent requests. An example may include

HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly)

ASP.NET Session State Overview

Community
  • 1
  • 1
scniro
  • 16,844
  • 8
  • 62
  • 106
  • 1
    Specifically, we had enabled session in web.api. Concurrent calls from the same session were locked due to requiring session access. – sonicblis May 05 '15 at 16:14
  • 1
    Just learning of the session specifics to your backend, I'll add to answer. Had to kind of take a best guess at your api implementation without all the details in question @sonicblis – scniro May 05 '15 at 16:16
  • @sonicblis: If session is what is blocking you the easiest solution is to look at changing the way you handle sessions. For instance [here is a post](http://johnculviner.com/asp-net-concurrent-ajax-requests-and-session-state-blocking/) detailing how to assign read only to the session, multiple read only requests can run in parallel. – Guvante May 05 '15 at 16:18
  • Yup already changed to `HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly)` and working beautifully. – sonicblis May 05 '15 at 16:26
1

I think your problem is caused either by browser or ASP.NET. In case it's ASP.NET, take a look at this thread:

C# Web API - Blocking

In case it's your browser, take a look at this thread with example code:

Why is my async ASP.NET Web API controller blocking the main thread?

Community
  • 1
  • 1
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • The BROWSER cause has to be emphasized. I was going nuts, couldn't figure out why the hell does my ASP.NET Core Web API processes requests on a single thread, and why does it seem that even await/async blocks... Then it turned out Chrome is simply delaying requests when more than one tab points to the same address. – Leaky Mar 18 '21 at 13:45