ASP。NET MVC和Ajax,并发请求?

时间:2022-09-03 04:14:58

AJAX newbie here!
At the moment in my ASP.NET MVC web app my AJAX requests appear to be getting batched or queued, im not sure.
No requests seem to be getting completed until the previous request has finished.
How do I go about getting the requests to return independantly?
I dont necessarily want someone to give me the answer but maybe some links to good tutorials or resources which could help. Thanks

AJAX新手!此刻在我的ASP。我的AJAX请求似乎正在批处理或排队,我不确定。在之前的请求完成之前,似乎没有任何请求被完成。如何获得独立返回的请求?我并不一定希望有人给我答案,但也许一些好的教程或资源的链接可以帮助我。谢谢

6 个解决方案

#1


35  

I'm expanding on Lachlan Roche's answer, which is correct.

我正在扩展拉克兰·罗奇的答案,这是正确的。

The ASP.NET framework will "single-thread" requests that deal with Session scope (a global resource), to prevent one request from interfering with another. In WebForms I think you can use the Page directive to specify that individual pages don't use Session and therefore don't need to treated synchronously like this.

ASP。NET框架将“单线程”请求处理会话范围(一个全局资源),以防止一个请求干扰另一个请求。在WebForms中,我认为可以使用Page指令来指定单个页面不使用会话,因此不需要像这样同步处理。

The problem is that in ASP.NET MVC all requests use Session, because it's used to implement TempData. You can disable session state entirely, as Lachlan Roche pointed out, or you can deal with this on a case-by-case basis.

问题是在ASP中。所有请求都使用会话,因为它用于实现TempData。您可以完全禁用会话状态,正如Lachlan Roche指出的,或者您可以根据具体情况处理它。

A possible solution might be to kick off your own background threads to process any long-running code, so that the initial request "completes" as quickly as possible.

一个可能的解决方案是启动您自己的后台线程来处理任何长时间运行的代码,以便初始请求尽可能快地“完成”。

#2


34  

ASP.NET will serially process requests on a per-session basis unless sessions are configured as disabled or read only in web.config via the enableSessionState attribute on the pages element.

ASP。NET将在每个会话的基础上串行处理请求,除非会话被配置为禁用或仅在web中读取。通过页面元素上的enableSessionState属性进行配置。

As this is a page setting, this will not affect MVC controllers and they will still be subject to serial request processing.

由于这是一个页面设置,因此不会影响MVC控制器,它们仍然需要进行串行请求处理。

Curiously, even with sessions disabled or set to readonly, we can still read and write session data. It seems to only affect the session locking that causes serial request processing.

奇怪的是,即使禁用会话或设置为只读,我们仍然可以读写会话数据。它似乎只会影响导致串行请求处理的会话锁定。

<system.web>
    <pages enableSessionState="ReadOnly"/>
</system.web>

Pages can also have an enableSessionState property, though this is not relevant to MVC views.

页面也可以具有enableSessionState属性,尽管这与MVC视图无关。

<%@ Page EnableSessionState="True" %>

#3


29  

With the release of ASP.MVC 3 you can now add an attribute to your controllers to mark the Session as readonly, which allows actions to be called concurrently from the same client.

随着ASP的发布。现在,您可以将一个属性添加到控制器中,以将会话标记为readonly,这允许同时从同一客户端调用操作。

Sessionless Controller Support:

Sessionless控制器支持:

Sessionless Controller is another great new feature in ASP.NET MVC 3. With Sessionless Controller you can easily control your session behavior for controllers. For example, you can make your HomeController's Session as Disabled or ReadOnly, allowing concurrent request execution for single user. For details see Concurrent Requests In ASP.NET MVC and HowTo: Sessionless Controller in MVC3 – what & and why?.

无会话控制器是ASP中另一个重要的新特性。净MVC 3。使用Sessionless控制器,您可以轻松地控制控制器的会话行为。例如,可以将HomeController的会话设置为禁用或只读,允许对单个用户并发请求执行。有关详细信息,请参见ASP中的并发请求。NET MVC和how: MVC3中的无会话控制器——什么和为什么?

- from this DZone article.

-来自DZone的文章。

By adding SessionState(SessionStateBehaviour.Disabled) to your controller, the runtime will allow you to invoke multiple actions concurrently from the same browser session.

通过将SessionState(sessionstatebehavior . disabled . disabled)添加到控制器中,运行时将允许您同时从同一浏览器会话中调用多个操作。

Unfortunately I don't think there is a way to mark an action so as to only disable the session when that action is called, so if you have a controller that has some actions that require the session and others that do not, you will need to move the ones that do not into a separate controller.

不幸的是我不认为有一种方法可以标记一个动作,只有关闭会话时,行动,所以如果你有一个控制器,有一些行为,需要会话和其他不需要的不进入一个单独的控制器。

In later versions of ASP MVC you can decorate individual controller classes with the SessionStateAttribute

在以后的ASP MVC版本中,您可以使用sessionstate属性来修饰单独的控制器类

[System.Web.Mvc.SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class MyController : Controller 
{
}

#4


2  

Since .NET Framework v3.0 released, you can use "SessionStateBehavior" enum with SessionStateAttribute:

自从。net Framework v3.0发布以来,您可以使用带有SessionStateAttribute的“SessionStateBehavior”枚举:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class MyController : BaseController { }

#5


-2  

Well Concurrent Request are more on browser dependent aswell if you fire suppose 10 concurrent request to an action Using AJax in Mozilla and same using IE 8 then you will find that Mozilla has style to fire one request wait for its response and then fire second and so on... for this is one by one basis whereas in IE * this fire about 6 concurrent request at a time to Server.

好并发请求更依赖于浏览器的同时如果你火上假设10并发使用AJax请求一个动作在同一使用Mozilla和IE 8然后你会发现,Mozilla风格解雇一个请求等待响应,然后火第二等等……因为这是一个接一个的基础,而在IE *中,每次向服务器发出大约6个并发请求。

So Concurrent Request are also dependent on browser type.

因此,并发请求也依赖于浏览器类型。

#6


-5  

I suggest using jQuery for your ajax needs with asp.net mvc, I have used it exclusively and it has been extremely easy.

我建议您使用jQuery来满足asp.net mvc的ajax需求,我只使用过它,而且非常简单。

As for tutorials I would look at this: http://docs.jquery.com/Ajax

至于教程,我想看看这个:http://docs.jquery.com/Ajax

There are tons of options to play with and I also suggest downloading firebug so you can watch requests launch from your page asynchronously and see if they fire and what they return etc.

有大量的选项可以使用,我还建议下载firebug,这样你就可以看到请求异步地从你的页面启动,看看它们是否启动,返回什么等等。

Like the other guy side, AJAX request are asynchronous and don't get queued up and they all return independently when they finish, so if you watch in firebug it will be easy to see what is going on behind the scenes and before the debugger gets hit

与其他方面一样,AJAX请求是异步的,不会排队,它们在完成时都是独立返回的,所以如果您观察firebug,就很容易看到幕后发生的事情,以及调试器被击中之前

#1


35  

I'm expanding on Lachlan Roche's answer, which is correct.

我正在扩展拉克兰·罗奇的答案,这是正确的。

The ASP.NET framework will "single-thread" requests that deal with Session scope (a global resource), to prevent one request from interfering with another. In WebForms I think you can use the Page directive to specify that individual pages don't use Session and therefore don't need to treated synchronously like this.

ASP。NET框架将“单线程”请求处理会话范围(一个全局资源),以防止一个请求干扰另一个请求。在WebForms中,我认为可以使用Page指令来指定单个页面不使用会话,因此不需要像这样同步处理。

The problem is that in ASP.NET MVC all requests use Session, because it's used to implement TempData. You can disable session state entirely, as Lachlan Roche pointed out, or you can deal with this on a case-by-case basis.

问题是在ASP中。所有请求都使用会话,因为它用于实现TempData。您可以完全禁用会话状态,正如Lachlan Roche指出的,或者您可以根据具体情况处理它。

A possible solution might be to kick off your own background threads to process any long-running code, so that the initial request "completes" as quickly as possible.

一个可能的解决方案是启动您自己的后台线程来处理任何长时间运行的代码,以便初始请求尽可能快地“完成”。

#2


34  

ASP.NET will serially process requests on a per-session basis unless sessions are configured as disabled or read only in web.config via the enableSessionState attribute on the pages element.

ASP。NET将在每个会话的基础上串行处理请求,除非会话被配置为禁用或仅在web中读取。通过页面元素上的enableSessionState属性进行配置。

As this is a page setting, this will not affect MVC controllers and they will still be subject to serial request processing.

由于这是一个页面设置,因此不会影响MVC控制器,它们仍然需要进行串行请求处理。

Curiously, even with sessions disabled or set to readonly, we can still read and write session data. It seems to only affect the session locking that causes serial request processing.

奇怪的是,即使禁用会话或设置为只读,我们仍然可以读写会话数据。它似乎只会影响导致串行请求处理的会话锁定。

<system.web>
    <pages enableSessionState="ReadOnly"/>
</system.web>

Pages can also have an enableSessionState property, though this is not relevant to MVC views.

页面也可以具有enableSessionState属性,尽管这与MVC视图无关。

<%@ Page EnableSessionState="True" %>

#3


29  

With the release of ASP.MVC 3 you can now add an attribute to your controllers to mark the Session as readonly, which allows actions to be called concurrently from the same client.

随着ASP的发布。现在,您可以将一个属性添加到控制器中,以将会话标记为readonly,这允许同时从同一客户端调用操作。

Sessionless Controller Support:

Sessionless控制器支持:

Sessionless Controller is another great new feature in ASP.NET MVC 3. With Sessionless Controller you can easily control your session behavior for controllers. For example, you can make your HomeController's Session as Disabled or ReadOnly, allowing concurrent request execution for single user. For details see Concurrent Requests In ASP.NET MVC and HowTo: Sessionless Controller in MVC3 – what & and why?.

无会话控制器是ASP中另一个重要的新特性。净MVC 3。使用Sessionless控制器,您可以轻松地控制控制器的会话行为。例如,可以将HomeController的会话设置为禁用或只读,允许对单个用户并发请求执行。有关详细信息,请参见ASP中的并发请求。NET MVC和how: MVC3中的无会话控制器——什么和为什么?

- from this DZone article.

-来自DZone的文章。

By adding SessionState(SessionStateBehaviour.Disabled) to your controller, the runtime will allow you to invoke multiple actions concurrently from the same browser session.

通过将SessionState(sessionstatebehavior . disabled . disabled)添加到控制器中,运行时将允许您同时从同一浏览器会话中调用多个操作。

Unfortunately I don't think there is a way to mark an action so as to only disable the session when that action is called, so if you have a controller that has some actions that require the session and others that do not, you will need to move the ones that do not into a separate controller.

不幸的是我不认为有一种方法可以标记一个动作,只有关闭会话时,行动,所以如果你有一个控制器,有一些行为,需要会话和其他不需要的不进入一个单独的控制器。

In later versions of ASP MVC you can decorate individual controller classes with the SessionStateAttribute

在以后的ASP MVC版本中,您可以使用sessionstate属性来修饰单独的控制器类

[System.Web.Mvc.SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class MyController : Controller 
{
}

#4


2  

Since .NET Framework v3.0 released, you can use "SessionStateBehavior" enum with SessionStateAttribute:

自从。net Framework v3.0发布以来,您可以使用带有SessionStateAttribute的“SessionStateBehavior”枚举:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class MyController : BaseController { }

#5


-2  

Well Concurrent Request are more on browser dependent aswell if you fire suppose 10 concurrent request to an action Using AJax in Mozilla and same using IE 8 then you will find that Mozilla has style to fire one request wait for its response and then fire second and so on... for this is one by one basis whereas in IE * this fire about 6 concurrent request at a time to Server.

好并发请求更依赖于浏览器的同时如果你火上假设10并发使用AJax请求一个动作在同一使用Mozilla和IE 8然后你会发现,Mozilla风格解雇一个请求等待响应,然后火第二等等……因为这是一个接一个的基础,而在IE *中,每次向服务器发出大约6个并发请求。

So Concurrent Request are also dependent on browser type.

因此,并发请求也依赖于浏览器类型。

#6


-5  

I suggest using jQuery for your ajax needs with asp.net mvc, I have used it exclusively and it has been extremely easy.

我建议您使用jQuery来满足asp.net mvc的ajax需求,我只使用过它,而且非常简单。

As for tutorials I would look at this: http://docs.jquery.com/Ajax

至于教程,我想看看这个:http://docs.jquery.com/Ajax

There are tons of options to play with and I also suggest downloading firebug so you can watch requests launch from your page asynchronously and see if they fire and what they return etc.

有大量的选项可以使用,我还建议下载firebug,这样你就可以看到请求异步地从你的页面启动,看看它们是否启动,返回什么等等。

Like the other guy side, AJAX request are asynchronous and don't get queued up and they all return independently when they finish, so if you watch in firebug it will be easy to see what is going on behind the scenes and before the debugger gets hit

与其他方面一样,AJAX请求是异步的,不会排队,它们在完成时都是独立返回的,所以如果您观察firebug,就很容易看到幕后发生的事情,以及调试器被击中之前