async/await with MVC4

The .NET 4.5 Framework introduced the new async/await asynchronous programming model. With ASP.NET MVC 4 comes the application of the async/await model to controller actions. A traditional ASP.NET MVC control action will be synchronous by nature; this means a thread in the ASP.NET Thread Pool is blocked until the action completes. Calling an asynchronous controller action will not block a thread in the thread pool. The greatest advantage of using this is to bring UI more responsive.
Well before .NET 4.5 (and MVC 4), we need to controller to inherit from AsyncController and implement this. With latest release this made simple and brought up to language level. To do Async in ASP.NET MVC, all you have to is return a Task and use the async keyword in your action methods.
As ASP.NET MVC 4 runs on top of .NET framework 4.5, we can use the magical async and await keywords to simplify this process. Using these keywords, we can easily create asynchronous controller actions just like we created methods. Following is a sample implementation of a controller action method:
Enough of theory, let’s walk the talk.

public class HomeController : Controller
{
    public async Task Index()
    {
        Task weatherInfoTask = WeatherApi.GetFollowersAsync();
        Task stockTickerTask = StockTickerApi.GetFollowingAsync();
        await Task.WhenAll(weatherInfoTask, stockTickerTask);
        ViewBag.WetherInfo = await weatherInfoTask;
        ViewBag.StockTicker = await stockTickerTask;
        return View();
    }
}

Here we first invoke both asynchronous Weather API calls and only then await them using Task.WhenAll. Finally, we use the Result property of each task to get the Weather and Stock Ticker info.
An async method will be run synchronously if it does not contain the await keyword. In the above code, we are calling a WCF services asynchronously. As a method marked as async must return a Task, the return type is modified to Task.await pauses the method until the operation completes.
Well, to conclude:
async: A method modifier that signals to the .NET language compiler that the marked method will run within its own thread, and that execution cannot continue past the point until the awaited asynchronous process is finished. Meanwhile, control is returned to the caller of the async method.
await: An operator that suspends the execution of the method to which it applies. When called, an asynchronous method synchronously executes the body of the function up until the first await expression on an awaitable instance that is not yet completed, at which point the invocation returns to the caller.
In this tutorial we learned how writing asynchronous controllers is a powerful optimization that has become much easier to implement since MVC 4 and C# 4.5.

Advertisements

WebDataGrid in MVC

I was planned to get in to some of the details of routing, areas stuff like that. But some of my friends who were working with some sample MVC using WebDataGrid, were asking me few questions on that. So I thought to come up with a code sample on that. But I did not got any evolution version of WebDataGrid from Infragistics to work with.So for time being we can see the sample of ASP.NET Grid view. The same way you can consume WebDataGrid too. But it is true that we can’t give samples for lot of in built features which coming with WebDataGrid like sorting, column resizing or whatever.
Ok come back to the sample that we already created. If we don’t have please visit the link. We are going to do following steps:
Create a view data model.
Add new action/method in controller class.
Modify the link button action in Site.Master.cs.
Create a new View and add a WebDataGrid/Grid on it.
First we have to create a view model. Go ahead and create a view model under Models folder. The code looks something like this:

public class GridViewModel
{
   public IList<EmployeeModel> listEmployee { get; set; }
}
public ActionResult SampleGridView()
{
    EmployeeDAL empDAL = new EmployeeDAL();
    List<EmployeeModel> employyesList = new List<EmployeeModel>();
    employyesList = empDAL.GetAllEmployees();
    var gvSample = new GridViewModel();
    gvSample.listEmployee = employyesList;
    return View(gvSample);
}

Thirdly Modify the link button to redirect to the new method/action we have created.
Html.ActionLink(“Employee”, “SampleGridView”, “Employee”)
Lastly we need to create a view which holds our WebDataGrid/grid. Go ahead and create a strongly typed view under employee sub folder:
Run the application and nothing is going to display on the new view that we created. Ok let’s see that what we have to do for displaying data in the grid. We need to open up a server side script now. The reason is that this is a data bound control and need data source properties to be set.
Now run the application and you will be able to see the grid populated with data. So for those who are using the code samples for WebDataGrid can still go ahead and play with rich behaviors properties that comes with Infragistics.