Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

use HttpClient, WebRequestHandler with Net 4.0 causing deadlock?

$
0
0

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 used anywhere.

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




Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>