27

Most Web API 2.0 methods I've seen return IHttpActionResult, which is defined as an interface that "defines a command that asynchronously creates a System.Net.Http.HttpResponseMessage".

I'm a little confused about what's going on when a method is returning async Task<IHttpActionResult>.

Why would you use one over the other? Or are these functionally identical - isn't IHttpActionResult already asynchronous?

4
  • 2
    You need to return a Task<IHttpActionResult> when you are using the async and await feature. Mar 17, 2015 at 14:37
  • @Romoku So the idea is that you'd only use async whenever you'd need to implement threading? In the context of Web API methods that perform simple CRUD operations, is there any functional difference between using a method that returns IHttpActionResult vs async Task<IHttpActionResult>?
    – alex
    Mar 17, 2015 at 14:48
  • Using async there may be performance improvements at scale, but it is negligible for most web applications. There is no functional difference between sync and async. Mar 17, 2015 at 15:07
  • @Romoku Thanks, this was a little bit confusing to me. If you want to post your comment as an answer, I'll mark it as accepted.
    – alex
    Mar 17, 2015 at 15:08

2 Answers 2

22

Your action may return an IHttpActionResult which performs the action asynchronously when the framework calls its ExecuteAsync.

But if you must first make some other async calls before creating and returning the result, then you're forced to change the signature to async Task<IHttpActionResult>. That's all it is.

If your controller action code doesn't use await then you can switch back to the simpler signature. However, the result you return will still be asynchronous.

To be clear, in both cases, you are using asynchronous code.

The performance benefit is that - provided all calls to the deepest level are async - a web server thread is not blocked during disk or network I/O, your server can handle more requests with fewer resources.

Think carefully before calling Wait or Result on a Task, or creating a Task yourself within ASP.NET code.

Two legitimate reasons to hand-code, intentional multi-threading or parallelism for web server code are:

  • when it receives minimal traffic but performs computational work, a call every so often to run a computation over data and you want to use all 16 cores.
  • when making >1 simultaneous calls to database shards or >1 other services, you'd make a task for each shard query up front and await them all.
17

The difference between using IHttpActionResult and async Task<IHttpActionResult> is whether any of your code utilizes the async and await feature. Many libraries like Entity Framework provide async versions of methods (e.g. SaveChangesAsync) that provide a slight performance increase. However, there are pitfalls to using async with Web API, so unless you understand many of the idiosyncrasies it is prudent to stick to the synchronous API.

Steven Cleary has a lot of information on his blog about the idiosyncrasies of async and await. To get started I advise looking at Don't block on async code.

1
  • 3
    He's not comparing an asynchronous API to a synchronous API, he's comparing an asynchronous API to a different type of asynchronous API.
    – Servy
    Mar 17, 2015 at 15:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.