简体   繁体   中英

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

I have a piece of code that loads data from a web service :

        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.

However, I also have a version of this embedded in a larger WPF application. 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. It seems as if the PostAsync is just not getting the returned response, and so not being able to progress. 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. It is just the capture of this response.

I had a similar problem recently, and it was solved by using ConfigureAwait(false) , like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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