IHttpActionResult vs async Task

前端 未结 2 1463
执念已碎
执念已碎 2021-02-02 10:03

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 10:29

    The difference between using IHttpActionResult and async Task 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.

提交回复
热议问题