Last post Feb 21, 2014 05:59 AM by PatriceSc
Feb 12, 2014 04:20 AM|ShiyiC|LINK
When using HttpClient with .net 4.0, I am sometimes seeing task canceled exception when accessing the Task.Result property . See the sample code below. I have seen threads regarding deadlock on accessing Task.Result with async/await method in a UI/asp.net
context. E.g. http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html. My code is not run in a UI/asp.net context. Since its .net 4.0, there is no async/await keyword
is there any possibility that the following code may run into issues with SynchronizationContext/deadlocks thus cause the task canceled exception? Or its just purely because of the httpclient time out ?
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.UseDefaultCredentials = true;
webRequestHandler.AllowPipelining = true;
// Create an HttpClient using the WebRequestHandler
HttpClient client = new HttpClient(webRequestHandler);
var resp = client.GetAsync(URL).Result; <= Here is where the task canceled exception thrown
Feb 21, 2014 05:59 AM|PatriceSc|LINK
Have you tried to inspect the exception to see possible inner exceptions ?
My code is not run in a UI/asp.net context.
Tell rather what it is rather than what it's not. Do you mean this is a console app ? Waht is the purpose of an async call and conusming immediatly the result...