问题描述
我使用HttpClient的张贴在.NET 4.0项目数据到远程服务。我不关心这个操作阻塞,所以我想我可以跳过ContinueWith或异步/等待和使用结果。
I'm using the HttpClient to post data to a remote service in a .NET 4.0 project. I'm not concerned with this operation blocking, so I figured I could skip ContinueWith or async/await and use Result.
调试时,我遇到了一个问题,即远程服务器无响应。当我通过code踩,这似乎是我的code正好停在第三行运行......当前堆栈指针线不再是黄色突出显示,并没有推进到下一行。它只是消失了。我花了一段时间才能认识到,我应该等待请求超时。
While debugging, I ran into an issue where the remote server wasn't responsive. As I stepped through the code, it seemed like my code just stopped running on the third line... the current stack pointer line stopped being highlighted yellow, and didn't advance to the next line. It just disappeared. It took me a while to realize that I should wait for the request to timeout.
var client = new HttpClient();
var task = client.PostAsync("http://someservice/", someContent);
var response = task.Result;
我的理解是,呼吁任务结果引起了code同步执行,表现得更像这样(我知道有没有邮政法HttpClient的):
My understanding was that calling Result on the Task caused the code to execute synchronously, to behave more like this (I know there is no Post method in the HttpClient):
var client = new HttpClient();
var response = client.Post("http://someservice/", someContent);
我不知道这是一件坏事,我只是想把我的头周围。难道真的凭借的事实,HttpClient的是直接返回任务,而不是结果,我的应用程序会自动采取异步的优势,甚至当我觉得我回避吗?
I'm not sure this is a bad thing, I'm just trying to get my head around it. Is it really true that by virtue of the fact that the HttpClient is returning Tasks instead of the results directly, my application is automatically taking advantage of asynchrony even when I think I'm avoiding it?
推荐答案
在Windows中,所有的I / O是异步的。同步API是只是一个方便的抽象。
In Windows, all I/O is asynchronous. Synchronous APIs are just a convenient abstraction.
所以,当你使用 HttpWebRequest.GetResponse
,实际发生的是I / O开始(异步),并且调用线程(同步)块,等待它完成。
So, when you use HttpWebRequest.GetResponse
, what actually happens is the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting for it to complete.
同样,当你使用 HttpClient.PostAsync(..)。结果
,在I / O开始(异步),并且调用线程(同步)块,等待它完成。
Similarly, when you use HttpClient.PostAsync(..).Result
, the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting for it to complete.
我通常建议人们使用等待
,而不是 Task.Result
或 Task.Wait
,原因如下:
I usually recommend people use await
rather than Task.Result
or Task.Wait
for the following reasons:
- 如果您在阻止
工作
这是一个的结果异步
方法,你可以的。 -
Task.Result
和Task.Wait
在AggregateException 包装的任何异常code>(因为这些API是由TPL留任)。这样的错误处理是比较复杂的。
- If you block on a
Task
that is the result of anasync
method, you can easily get into a deadlock situation. Task.Result
andTask.Wait
wrap any exceptions in anAggregateException
(because those APIs are holdovers from the TPL). So error handling is more complex.
不过,如果你意识到这些局限性,也有一些地方阻挡在工作
可能是有用的(例如,在一个控制台应用程序的<$ C $情况C>主要)。
However, if you're aware of these limitations, there are some situations where blocking on a Task
can be useful (e.g., in a Console application's Main
).
这篇关于而任务的结果等待着会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!