HttpClient SendAsync thread call back dies before parsing response
-
11-06-2021 - |
Question
I am testing out the httpClient sendAsync method. Synchrnous call it works fine. But, when I try to do asynchronous http requests, the task terminated before I process the message. Any body encountered with this issue?
This synchronous call works
HttpRequestMessage httprequest = new HttpRequestMessage(httpmethod, "http://www.google.ca");
var result = _httpClient.SendAsync(httprequest, HttpCompletionOption.ResponseContentRead, cancellationToken);
HttpResponseMessage response = result.Result;
But the Asynchrnous call doesn't work. The thread dies when I try to parse the response.
HttpRequestMessage httprequest = new HttpRequestMessage(httpmethod, "http://www.google.ca");
var result = await _httpClient.SendAsync(httprequest, HttpCompletionOption.ResponseContentRead, cancellationToken);
HttpResponseMessage response = result;
any help is appreciated.
La solution
If you are using VS11 Beta, then make sure your unit test returns Task
, not void
.
If you're using VS2010 or VS11 Dev Preview, then you can use my Async Unit Tests project.
Full details are on my blog, but it boils down to this:
async
will return when it has to asynchronously wait.- In a unit test, it's returning to the test runner, which sees the test complete.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow