MVC控制器动作中的异步/等待 [英] async/await in MVC controller's action

查看:132
本文介绍了MVC控制器动作中的异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.net MVC控制器中有一个Index操作.此操作(除其他事项外)调用一个私有操作,该操作对具有大量行的SQL表进行计数.返回的数字将插入到view bag属性中.

I have an Index action in ASP.net MVC controller. This action, calls (among other things) a private action that do a count on a SQL table with large set of rows. The returned number will be inserted in a view bag property.

public ActionResult Index() 
{
    // do things
    ViewBag.NumberOfRows = NumberOfRows();
    return View();
}

private string NumberOfRows()
{
    // sql connection and row count
    return numberOfRows;
}

这可行,但是直到执行一切,甚至行数,我都看不到索引页面. 相反,我将立即执行Index动作,即使私有功能尚未完成也是如此.比完成计数时,要为view bag属性设置一个值. 现在,我已经做到了:

This works but I can't see the Index page until everything is executed, even the row count. I would, instead, Index action to immediately complete, even if private function hasn't been completed yet. Than when count has been completed set a value to the view bag property. Right now I've done this:

private async Task<string> NumberOfRows()
{
    SqlConnection connection = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand();
    SqlDataReader reader;

    cmd.CommandText = "SELECT SUM (row_count) FROM sys.dm_db_partition_stats WHERE object_id=OBJECT_ID('aTable') AND (index_id=0 or index_id=1)";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;

    await connection.OpenAsync();

    reader = await cmd.ExecuteReaderAsync();
    string numberOfRows = "N/A";
    while (await reader.ReadAsync())
    {
        numberOfRows = reader.GetInt64(0).ToString();
    }

    connection.Close();

    return numberOfRows ;
}

public async Task<ActionResult> Index(FormCollection form){
    // do things;
    ViewBag.NumberOfRows = await NumberOfRows();
    return View();
}

这有效.但这真的是异步的吗?我想念什么吗,还有其他方法吗?

This works. But is this really async? Am I miss something, there are other way of doing this?

推荐答案

它的async调用,但要理解的一件事是在这种情况下执行控制器操作async时:thread(of asp.net thread池)哪个处理请求返回到线程池(asp.net请求线程池).

Its async call but one important thing to understand here is when you make your controller action async in that case : thread(of asp.net thread pool) which handling request return to thread pool (asp.net request thread pool ).

这意味着它释放thead池的线程来处理更多请求(这意味着异步控制器动作仅有助于处理更多请求,这并不意味着它减少了您的处理时间,而只是使您的服务器更具响应性).一旦在异步/等待状态下完成操作,请求线程池中的新线程便会进行进一步处理.

That means it release thread of thead pool to handle more request (It means async controller action just help to handle more request it doesnt mean that it decrease your time of processing, It just make your server more responsive). once operation under async/await is get completed new thread from request thread pool does further processing.

如果您想要真正的异步页面,即希望使页面更具响应性,我建议使用jQuery的.ajax()函数或使用Asp.net MVC中可用的ajax扩展进行调用.

If you want real async page i.e. want to make your page more responsive I suggest make call using .ajax() function of jQuery or using ajax extesion available in Asp.net MVC.

这篇关于MVC控制器动作中的异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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