title: NullReferenceException in System.Threading.Tasks calling HttpClient.GetAsync(url)
body: <p>I have a strange problem in my MVC 4.0 application. I use REST web services (Amazon Associate) . I created a method, which I use from everywhere. The shortened version is this:</p>
  
  <pre><code>    private async Task&lt;XElement&gt; GetRequest(string url)
      {
          string myresponse;
          HttpResponseMessage response = null;
          HttpClient client = new HttpClient();            
          try
          {
              response = await client.GetAsync(url);
              myresponse = response.Content.ToString();
              if (myresponse.Contains("503"))
              {
                  myTrace.WriteLine("503 Sleep.....");
                  Thread.Sleep(3000); // looks like amazon us does not like fast requests....
                  return await GetRequest(url); //restart after pausing....
              }
          }
          catch (TaskCanceledException ex)
          {
              myTrace.WriteLine("TaskCancelled From GetRequest: " + ex);
              return null;
          }
  
          catch (HttpRequestException ex)
          {
              myTrace.WriteLine("RequestException Sleep.....");
              Thread.Sleep(300000); // 5 minutes de pause 
          }
  
          catch (Exception ex)
          {
              myTrace.WriteLine("From GetRequest: " + ex);
              return null;
          }
  
          try
          {
              XElement content = await response.Content.ReadAsAsync&lt;XElement&gt;();
              response.Dispose();
              client.Dispose();
              return content;
          }
          catch (Exception)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>Nothing fancy, it does work perfectly well....But, now, on a specific call, it bombs on <code>client.GetAsync(url)</code>. At first I suspected something in the url to be wrong, so I grabbed it from a debugger session and pasted it directly in my browser, got the expected answer...</p>
  
  <p>So, nothing wrong with the URL. Made a little Unit Test, works just fine with that same specific URL...</p>
  
  <p>As it bombs in the debugger, difficult to see what's wrong. (There are no exceptions thrown!). Finally, I saw with IntelliTrace that there ARE exceptions, seemingly inside <code>System.Threading.Tasks</code>. Difficult to pin point, as the call Stack is a bit confusing for my NON expert eyes....</p>
  
  <p>Here is the call stack I get from a previous pass in the code:</p>
  
  <pre><code>&gt;   System.Web.dll!System.Web.ThreadContext.AssociateWithCurrentThread(bool setImpersonationContext = {unknown})    C#
  System.Web.dll!System.Web.HttpApplication.OnThreadEnterPrivate(bool setImpersonationContext = {unknown})    C#
  System.Web.dll!System.Web.HttpApplication.OnThreadEnter()   C#
  System.Web.dll!System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter()  C#
  System.Web.dll!System.Web.Util.SynchronizationHelper.SafeWrapCallback(System.Action action = {unknown}) C#
  System.Web.dll!&lt;&gt;c__DisplayClass9.AnonymousMethod(System.Threading.Tasks.Task _ = {unknown})    C#
  mscorlib.dll!System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()  C#
  mscorlib.dll!System.Threading.Tasks.Task.Execute()  C#
  mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj = {unknown})   C#
  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext = {unknown}, System.Threading.ContextCallback callback = {unknown}, object state = {unknown}, bool preserveSyncCtx = {unknown})   C#
  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext = {unknown}, System.Threading.ContextCallback callback = {unknown}, object state = {unknown}, bool preserveSyncCtx = {unknown})   C#
  mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot = {unknown})    C#
  mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution = {unknown}) C#
  mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() C#
  mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()    C#
  mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() C#
  </code></pre>
  
  <p>Anyway, this looks definitely linked to Tasks, Async, Background workers, etc... Is there a good way to "clear" all other running tasks, to avoid this problem?</p>
  
  <p>Thanks for your help,
  Bernard.</p>