Using Asynchronous Methods in ASP.NET MVC 4

In .net 4.5 , there is a new feature is called  asynchronous method calling , action methods that return an object of type  Task<ActionResult> , MVC 4 supports Task based Programming , it is inheriting from namespace called System.Threading.Tasks , The .NET Framework 4.5 builds on this asynchronous support with  the await and async keywords that make working with Task objects which is  much less complex than previous asynchronous approaches. 

public async Task<List<Gizmo>> GetGizmosAsync()
{
var uri = Util.getServiceUri(“giz”);
using (HttpClient httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(uri);
return (await response.Content.ReadAsAsync<List<Gizmo>>());
}
}

this action also cancel based ,Task<ActionResult> is also cancel able  .

This approaches will follow this steps

1)It will return as Task based programming.

[AsyncTimeout(100)]
public async Task<ActionResult> GetStudentAsync()

here user can set timeout of the async calling declaring the attribute of AsyncTimeout

2)It will use async keyword for declaring task

3)The async method will use await keyword for time to complete the calling.

 

You Might Also Like

Leave a Reply