简体   繁体   English

HttpClient.SendAsync是否显然崩溃或阻止?

[英]HttpClient.SendAsync APPARENTLY crashing or blocking?

I have a real puzzler here, but first some background. 我在这里有一个真正的难题,但首先要有一些背景知识。

I am developing (while learning C# and .Net) a WPF application that 我正在开发(在学习C#和.Net的同时)一个WPF应用程序,该应用程序

  • uses HttpClient to connect to a piece of network equipment 使用HttpClient连接到一台网络设备
  • downloads a log file as a .csv (after logging in) 将日志文件下载为.csv(登录后)
  • massages the log entries and present them in a GridView 整理日志条目并将其显示在GridView中

The way I've been learning is to write each component (login, get_log, process_log, etc) individually as Console apps, then bringing them all together within a WPF framework. 我一直在学习的方法是将每个组件(登录,get_log,process_log等)分别作为控制台应用程序编写,然后将它们全部集成到WPF框架中。

As you can imagine, I was able to get all the individual functions working independently; 如您所料,我能够使所有单个功能独立工作。 but when I put all the pieces together it seems the application can't get past the initial connection - essentially a GET of http://10.0.0.1/login.html?/main.html . 但是当我将所有部分放在一起时,似乎应用程序无法通过初始连接-本质上是http://10.0.0.1/login.html?/main.html的GET。 I stepped through the WPF version and found it went into the client.SendAsync() call but never came back out. 我逐步浏览了WPF版本,发现它进入了client.SendAsync()调用,但从未退出过。 (The HttpClient instance's timeout is set for the default 100 seconds and I waited MUCH longer than that.) (HttpClient实例的超时设置为默认的100秒,我等待的时间比这更长。)

While the program is "hung" the app's main window is completely unresponsive: none of the minimize, maximize or close controls work. 当程序“挂起”时,应用程序的主窗口完全没有响应:最小化,最大化或关闭控件均无效。 I can't even drag the window around though I am able to click on other windows, and click on it again to regain focus. 我什至不能拖动窗口,尽管我可以单击其他窗口,然后再次单击以重新获得焦点。

When I watch the network traffic with Wireshark I see my app sending the GET request and the remote device responding with a 200 OK and the appropriate HTML content, but nothing else after that. 当我用Wireshark观察网络流量时,我看到我的应用程序发送GET请求,并且远程设备以200 OK和适当的HTML内容进行响应,但此后没有其他响应。

I have double- and triple-checked the code between the WPF version and the Console version (for this particular section) and they are identical. 我已经仔细检查了WPF版本和控制台版本(对于此特定部分)之间的代码,它们是相同的。

So, my questions are: 因此,我的问题是:

  1. Has anyone seen anything like this before? 有人看过这样的东西吗?
  2. Any suggestions for how I might go about figuring out what's going on? 关于如何确定发生了什么的任何建议?

Has anyone seen anything like this before? 有人看过这样的东西吗?

Yes

Any suggestions for how I might go about figuring out what's going on? 关于如何确定发生了什么的任何建议?

Never use .Wait() or .Result on async method. 切勿在异步方法上使用.Wait().Result Use await instead. 请改用await On console application this 'might' not be a problem since it doesnt have a synchronization context. 在控制台应用程序上,此“可能”没有问题,因为它没有同步上下文。 However on WPF application you will almost guarantee to run into deadlock if this is run on the UI thread. 但是在WPF应用程序上,如果它在UI线程上运行,几乎可以保证会陷入死锁。

If you are interested in more details Stephen Cleary got a great explanation on his blog 如果您对更多细节感兴趣,Stephen Cleary在他的博客中得到了很好的解释。

Async/await solve blocking issue 异步/等待解决阻塞问题

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri("http://localhost");
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, "users");
            httpRequest.Content = new StringContent("{name:'admin'}");
            var httpResponse = await httpClient.SendAsync(httpRequest);
            //process http response
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM