C# Web API - Blocking

2019-08-16 10:39发布

We have built a web service that loads data from an external API. A standard page contains 6-8 modules, all loading data from different resources from the API. It looks like this(each block loads from a different url, e.g. /cars, /phones, /games... enter image description here

One call to the external API can take like 10-15 second(not unusual at all)...

When loading the page each block will fire an Ajax call with jQuery(we are using backbone) that asks our own API(it's like a proxy, only routes the call forward to the external API) for data.

From the beginning we thought "Ah nice, ajax will solve all our problems"...and it does, on the client side...However, we are not using async or anything like that on the server side so I guess you can see what problems we are facing...that's right, blocking threads.

We cant have more than 2-3 concurrent users because of this. We are using c# MVC together with Web Api and on the frontend we are using Backbone.

My question is; will a async controllers solve our problems? Or should we implement this in another way? Some of you will say "just cache the data and you will be fine". And yes, we are caching the data but it will still block up all the threads(not as long as the first request, but still noticeable)

I see quite a few different solutions for this:

So, what do you guys recommend?

Also, I found this: Why is my async ASP.NET Web API controller blocking the main thread? Where the answer suggesting that it's a problem with the browser itself, that it doesn't allow multiple concurrent calls before getting a response from the first request?

1条回答
爷、活的狠高调
2楼-- · 2019-08-16 11:06

The solution was really simple. On my basecontroller, I put this attribute: SessionState(SessionStateBehavior.Disabled)

ASP.NET puts a lock on all requests, which means if you do concurrent requests, they will not run in parallel.

You can read more about it in this article from Microsoft.

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.)

查看更多
登录 后发表回答