asp.net mvc 4 控制器并行执行多个 ajax 调用 [英] asp.net mvc 4 controller execute multiple ajax calls in parallel

查看:26
本文介绍了asp.net mvc 4 控制器并行执行多个 ajax 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET MVC 4 控制器,它的方法是通过 ajax 调用的.

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

问题是ajax请求是由控制器顺序处理的.这会导致性能问题,因为加载页面的时间是所有 ajax 请求的总和,而不是最长的 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.

为了演示这一点,我在第一个(ReadOverview8Week")方法中放置了一个断点.这些方法中的每一个都需要大约 600 毫秒来执行个性化.

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

如何让控制器同时响应所有三个请求?我正在使用 iis 8.

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

这是ajax请求(来自kendo ui dataSource)

This is the ajax request (from kendo ui dataSource)

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

谢谢.

推荐答案

您面临的问题是由 ASP.NET 管理会话的方式引起的.这是 ASP.NET 会话状态概述(并发请求和会话状态部分):

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

对 ASP.NET 会话状态的访问是每个会话独占的,这意味着如果两个不同的用户并发请求,则同时授予对每个单独会话的访问权限.但是,如果为同一个会话发出两个并发请求(通过使用相同的 SessionID 值),第一个请求将获得对会话信息的独占访问权.第二个请求只有在第一个请求完成后才会执行.

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.

如果您的操作不需要访问会话,您可以解决您的问题.如果是这种情况,您可以使用 SessionStateAttribute 属性:

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.

如果您的操作只需要对会话进行读取访问,您可以尝试使用 SessionStateBehavior.ReadOnly 值.这不会导致排他锁,但请求仍需等待读写请求设置的锁.

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.

这篇关于asp.net mvc 4 控制器并行执行多个 ajax 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆