11

I have an asp.net MVC 4 controller thats methods are called via ajax.

The problem is that the ajax requests are handled sequentially by the controller. This causes performance issues as the time to load the page is the sum of all ajax requests and not the longest ajax request.

To demonstrate this, I put a break point in the first ("ReadOverview8Week") method. Each of these methods take ~600ms to execute individuality.

How can I make the controller respond to all three requests in parallel? I am using iis 8.

This is the ajax request (from kendo ui dataSource)

.DataSource(dataSource => dataSource.Ajax()
   .Read(read => read.Action("ReadAllSitesOverview", "AbuseCase").Type(HttpVerbs.Get))

Thanks.

2
  • Include the code for your ajax requests. Commented Mar 21, 2013 at 13:58
  • The ajax requests in this example were from a kendo datasource. Commented Mar 21, 2013 at 14:00

1 Answer 1

21

The issue you are facing is caused by the way ASP.NET is managing session. Here is the most important part from ASP.NET Session State Overview (Concurrent Requests and Session State section):

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.

You can resolve your issue if your actions doesn't require access to session. If that is the case, you can decorate the controller with SessionStateAttribute attribute:

[SessionState(SessionStateBehavior.Disabled)]

This way the controller will not have to wait for session.

If your actions require only read access to session, you can try using the SessionStateBehavior.ReadOnly value. This will not result in an exclusive lock but the request will still have to wait for a lock set by a read-write request.

Sign up to request clarification or add additional context in comments.

10 Comments

Putting this on the controller fix my issue! Thank you.
How to do this in web api. I noticed ih I hit api multiple times in short period of times they are executed much much slower.
@vlado If you are using ASP.NET Web API the problem is the same (it doesn't affect self host). The only way to change it is through web.config. Also making sure your methods are async helps a bit.
it helps a bit althought data access is synchronous? I thought ajax request are totaly independent on each other. I still didn't find simple explanation why async request are not totaly "async" and why async web api method helps if the requests are already asnyc(independent on each other).
@Vlado AJAX requests are "async" from browser point of view, not the server. On the server all request are the same and in context of ASP.NET web application session is a synchronization root unless you disable it. Making ApiController method async (and by this I mean not just putting async keyword in front of it, but also using async methods to accessing database etc) results in the method not blocking the thread while waiting for external resources - it helps a bit ;). I suggest we continue this discussion via email if you want, in order not to spam the comments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.