Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async/await in MVC controller's action

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;
}

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?

like image 698
BAD_SEED Avatar asked Jul 06 '15 11:07

BAD_SEED


People also ask

What is use async controller actions?

The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. It does not mean it will take lesser time to complete the action.

How do you call async method in controller?

Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %> .

How do you call async task ActionResult?

1 Answer. Show activity on this post. It is best for you to use async/await down the whole call stack which is possible with MVC. Mark your Action as async and then use the await keyword to wait for the results from the CsvReader method.


1 Answers

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

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.

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.

like image 181
Pranay Rana Avatar answered Sep 28 '22 05:09

Pranay Rana