【发布时间】:2016-03-16 04:48:43
【问题描述】:
我在 ApiController 中实现了(我认为是)一个简单的异步方法,如下所示:
public class CommentsController : ApiController
{
private static readonly IList<CommentModel> Comments;
static CommentsController()
{
// Note: Removed to abbreviate - populate comments manually here.
}
[ResponseType(typeof (IList<CommentModel>))]
[HttpGet]
public async Task<IHttpActionResult> GetAllComments()
{
return await Task.Factory.StartNew(() =>
{
Thread.Sleep(10000); // Breakpoint #1
return Ok(Comments); // Breakpoint #2
});
// Breakpoint #3
}
}
注意我在上面放置的断点。
当我点击继续时,我预计会发生在#1,线程等待,但在这个阶段流程继续通过#3。
然后当睡眠结束后,再次继续并在#2处休息。
但是在调试过程中它似乎是同步的。
我的问题首先是这真的是异步的,我如何调试它来验证,或者通过单元测试?
【问题讨论】:
-
附言。我直接从浏览器访问 api - 也许这就是问题所在?
标签: c# asp.net-web-api visual-studio-2015 async-await