如何使ASP.NET MVC控制器方法异步 [英] How to make ASP.NET MVC Controller Methods Async

查看:72
本文介绍了如何使ASP.NET MVC控制器方法异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向各种MVC控制器启动多个ajax调用,以加载页面的不同部分.但是,似乎这一次到达控制器时,一次只能运行一次.我猜这是因为默认情况下ASP.Net MVC控制器是同步的?我还测试了在2个浏览器标签上加载页面,第二个标签始终等待第一个标签.

I'm launching multiple ajax calls to various MVC controllers to load different parts of my page. However it seems that when this gets to the controller only one runs at a time. I'm guessing this is because by default ASP.Net MVC controllers are synchronous? I've also tested loading a page on 2 browser tabs and the second tab always waits for the first.

为了解决这个问题,我尝试使有问题的控制器方法异步.我通过执行以下操作完成了

To get round this I've attempted to make the controller methods in question asynchronous. I've done this by doing the following

  1. 将异步附加到控制器方法名称
  2. 使控制器方法返回异步任务
  3. 使用Task.Factory.StartNew方法在单独的线程中执行该方法中的工作.

例如,有问题的控制器方法现在看起来像这样……

For example the controller methods in question now look like this...

    public async Task<JsonResult> GetUser(int userId)
    {
        var result = await Task.Factory.StartNew(() => Task.Run(() =>
        {                
            return userService.GetUser(userId);
        })).Result;
        return new JsonResult()
        {
            Data = result,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    } 

但是它似乎仍然是同步的.我是否缺少某些东西或完全以错误的方式进行此操作?我还没有真正使用过任务库,所以可能会丢失一些大东西?

However it still seems to be synchronous. Am I missing something or going about this completely the wrong way? I've not really used the Task Library much so may be missing something big?

推荐答案

不,您的假设很可能是错误的.您的问题可能是两个问题中的一个(或两个).

No, your assumptions are most likely wrong. Your problem is likely one (or both) of two problems.

首先,大多数Web浏览器都有请求限制,一次只能允许对同一服务器的一定数量的请求.

First, most web browsers have request limits that only allow a certain number of request to the same server at a time.

第二,您可能会遇到Session对象的限制,该限制会导致使用会话的多个请求被序列化,因为Session本身不是多线程的.

Second, you are probably running into a limitation of the Session object that causes multiple requests that use session to be serialized, because Session is not, itself, multi-threaded.

请参见 http://tech-journals.com/jonow/2011/10/22/the-downsides-of-asp-net-session-state

简短的答案是,如果您在动作方法中不使用会话,只需将其添加到方法中...

The short answer is that if you don't use session in your action method, simply add this to the method...

[SessionState(SessionStateBehavior.Disabled)]
public class AjaxTestController : Controller
{        
    //...As above
}

如果您只需要阅读会话,请执行以下操作:

If you only need to read the session, then do this:

[SessionState(SessionStateBehavior.ReadOnly)]
public class AjaxTestController : Controller
{        
    //...As above
}

尽管由于不同的浏览器有特定的请求限制,但是您对浏览器的限制却无能为力.通常可以使用注册表(或浏览器配置)条目来更改这些设置,但是在大多数情况下,您不能强迫用户这样做.

There's not much you can do about the browser limitations though, since different browsers have specific request limits. These can be changed with registry (or browser config) entries (usually), but you can't force your users to do that in most cases.

这篇关于如何使ASP.NET MVC控制器方法异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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