简体   繁体   English

为什么HttpClient.PostAsync在WPF环境中无法返回?

[英]Why does HttpClient.PostAsync fail to return in a WPF environment?

I have a piece of code that loads data from a web service : 我有一段代码可以从Web服务加载数据:

        client = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(60)};
        var post = await client.PostAsync(url, new FormUrlEncodedContent(parameters));

When I run this in the stand-alone applications - one of which is WPF, the other is WinForms - this works perfectly OK. 当我在独立应用程序中运行此程序时(其中一个是WPF,另一个是WinForms),可以正常运行。

However, I also have a version of this embedded in a larger WPF application. 但是,我在较大的WPF应用程序中也嵌入了此版本。 In both cases, when the embedded version is used, the application hangs on the post line. 在两种情况下,当使用嵌入式版本时,应用程序都将挂在后行上。

I have checked at the web service end, and on fiddler, and it is definitely sending the request, and the response is being returned. 我已经在Web服务端和Fiddler上进行了检查,它肯定是在发送请求,并且正在返回响应。 It seems as if the PostAsync is just not getting the returned response, and so not being able to progress. 似乎PostAsync只是没有获得返回的响应,因此无法继续执行。 Is there something I need to do to enable this to function? 我需要做些什么才能使它起作用吗?

All of the HTTP request and response looks fine, so I don't think it is anything to do with that. 所有的HTTP请求和响应看起来都很好,所以我认为与此无关。 It is just the capture of this response. 这只是该响应的捕获。

I had a similar problem recently, and it was solved by using ConfigureAwait(false) , like this: 我最近有一个类似的问题,可以通过使用ConfigureAwait(false)来解决,如下所示:

var post = await client
                 .PostAsync(url, new FormUrlEncodedContent(parameters))
                 .ConfigureAwait(false);

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

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