[英]HttpClient's PostAsync method blocks even though ConfigureAwait(false) is used
I've spent many hours trying to figure this one out, but unfortunately to no avail. 我花了很多时间试图弄清楚这一点,但不幸的是没有结果。 I'd be grateful for any help.
我将不胜感激。
The problem is that the PostAsync
call blocks event though I use await
and ConfigureAwait(false)
. 问题是尽管我使用
await
和ConfigureAwait(false)
,但PostAsync
调用阻止了事件。 The below handler is called on the UI thread: 在UI线程上调用以下处理程序:
private async void OnApplicationLaunching(object sender, LaunchingEventArgs e)
{
using (var httpClient = new HttpClient())
{
var content = new List<KeyValuePair<string, string>>();
var urlEncodedContent = new FormUrlEncodedContent(content);
await httpClient.PostAsync("url address", urlEncodedContent).ConfigureAwait(false);
}
}
Edit 1 编辑1
Edit 2 编辑2
Here's another code sample which does not differ alot from previous one, but beter resembles the code samples from Stephen's blog 这是另一个代码示例,与以前的代码示例没有很大不同,但更像是Stephen博客中的代码示例。
Section "Preventing the Deadlock" in http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html中的 “防止死锁”部分
Section "Avoiding Context" in http://blog.stephencleary.com/2012/02/async-and-await.html http://blog.stephencleary.com/2012/02/async-and-await.html中的 “避免上下文”部分
private async void OnApplicationLaunching(object sender, LaunchingEventArgs e)
{
await this.SendSth();
// here I want to know for sure that data has already been sent
}
private async Task SendSth()
{
using (var httpClient = new HttpClient())
{
var content = new List<KeyValuePair<string, string>>();
var urlEncodedContent = new FormUrlEncodedContent(content);
await httpClient.PostAsync("some url", urlEncodedContent).ConfigureAwait(false);
}
}
By the way, thanks Stephen for your great articles. 顺便说一句,感谢斯蒂芬的出色文章。 They help alot.
他们有很多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.