Your official information source from the .NET Web Development and Tools group at Microsoft.
Entity Framework 6 introduces support for the .NET 4.5 asynchronous programming pattern using the async and await keywords. And in Visual Studio 2013 RC we’re making it easier for you to take advantage of this new capability by optionally generating asynchronous code when you scaffold MVC and Web API controllers.
First thing first. Why would you build an asynchronous controller? Well, asynchronous programming is an important part of building scalable, robust, and responsive web applications.
A web server has a limited number of threads available, and in high load situation all of the available threads might be in use. When that happens, the server can’t process new requests until the threads are freed up. With synchronous code, many threads may be tied up while they aren’t actually doing any work because they’re waiting for I/O to complete. With asynchronous code, when a process is waiting for I/O to complete, its thread is freed up for the server to use for processing other requests. As a result asynchronous code enables server resources to be used more efficiently, and the server is enabled to handle more traffic without delays.
For more information about asynchronous programming, see the tutorial Using Asynchronous Methods in ASP.NET MVC 4[ii] and the video How to Build ASP./NET Web Application Using Async[iii].
The MVC and Web API controller scaffolders have a new Use async controller actions check box.The selection will be remembered next time you use the scaffolder.
MVC Controller using Entity Framework
Web API 2 Controller / OData Controller using Entity Framework
Following is the sample code generated by the scaffolders. The scaffolders generate code to read, create, update and delete data, using the Entity Framework data context.
When you select the async option, the method is marked as “async”, the EF async API is called, and the “await” keyword is used when calling the EF API.
Can you do a follow-up post on how to unit test these controllers with async actions? That has caused me some pain in the past
+1. Explanation of unit tests or it might as well have not happened.
Guys, it is all about the frameworks that you are using. Async actions doesn't complicate too much testing. It is generally the same as for synchronous controllers. Simple example with MSTests and Moq frameworks:
[TestMethod] // [Fact] for XUnit framework
public async Task YourTestName()
// mock/stub your async method, use a framework or do it yourself - Task.FromResult() is the key
// SomeMethodAsync() returns Task<string>
var fakeRepository = new Mock<ISomeRepository>();
fakeRepository.Setup(m => m.SomeMethodAsync()).Returns(Task.FromResult("your object to return"));
// constructor injection, or other technique (e.g. property injection)
var target = new SomeController(fakeRepository.Object);
// SomeMethodAsync() uses mocked/stubed ISomeRepository inside
ActionResult result = await target.SomeMethodAsync();
// your assertion
Sry for mistake while typing, I meant Action, not Method.
// SomeAction() uses mocked/stubed ISomeRepository inside (calls SomeMethodAsync())
ActionResult result = await target.SomeAction();
I'm the author of this article. I'd like to follow up with the comments.
First, I'd like to thank Krystian for responding :) Indeed there is nothing special about testing in the context of the async. You can always use the "Result" property on Task to force a synchronous call so all you old testing tricks still work.
However unit testing an async controller is still a great topic. Your messages are received and I'm working a post on this topic. Please stay tuned.
Troy Xueyuan Dai