简体   繁体   English

即使使用ConfigureAwait(false),HttpClient的PostAsync方法也会阻塞

[英]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) . 问题是尽管我使用awaitConfigureAwait(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

  • I use WindowsPhone 8.0 silverlight 我使用WindowsPhone 8.0 silverlight
  • In my opinion it is a deadlock. 我认为这是一个僵局。 The execution is blocked until the exception appears with "task got canceled" message 执行被阻止,直到出现异常消息“任务已取消”
  • I am not interested in "fire and forget". 我对“生与死”不感兴趣。 I need to be sure that the data is sent before allowing further execution. 我需要确保在允许进一步执行之前已发送数据。

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. 他们有很多帮助。

From my understanding of asynchronous programming in C#, ConfigureAwait should be called in library. 根据我对C#中异步编程的理解,应该在库中调用ConfigureAwait

See this article from Stephen Cleary. 请参阅Stephen Cleary的这篇文章 Check this also. 也检查一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#HttpClient-PostAsync不返回(即使使用ConfigureAwait) - C# HttpClient - PostAsync doesn't return (even with ConfigureAwait) PostAsync方法的HttpClient错误 - HttpClient error with PostAsync method 即使被调用方方法在内部执行ConfigureAwait(false),ConfigureAwait(true)始终返回原始线程吗? - Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? 即使使用ConfigureAwait,也会在WebAPI死锁中同步调用异步方法(false) - Synchronously calling async method in WebAPI deadlocks even with ConfigureAwait(false) C# HttpClient PostAsync 永远阻塞 - C# HttpClient PostAsync blocks forever 仍然对与C#中的GetAwaiter和GetResult一起使用的ConfigureAwait(false)感到困惑。 使死锁或方法不返回 - Still confused on ConfigureAwait(false) used with GetAwaiter and GetResult in C#. Getting a deadlock or method not returning 库类型的远程Web服务调用和HttpClient ConfigureAwait(false) - Library type remote web service calls and HttpClient ConfigureAwait(false) HttpClient PostAsync方法引发聚合异常 - HttpClient PostAsync method throw Aggregate Exception System.Net.Http.HttpClient.PostAsync 阻塞并且永不返回 - System.Net.Http.HttpClient.PostAsync blocks and never returns 为什么在使用 COnfigureAwait(false) 时我有一个填充的 HttpContext? - Why do I have a filled HttpContext when COnfigureAwait(false) is used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM