asp.net mvc 4 controller execute multiple ajax calls in parallel

一笑奈何 提交于 2019-12-04 09:23:52

问题


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.

http://i.stack.imgur.com/HhtEX.png

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.


回答1:


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.



来源:https://stackoverflow.com/questions/15549519/asp-net-mvc-4-controller-execute-multiple-ajax-calls-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!