title: HttpClient System.Threading.Tasks.TaskCanceledException: 'The operation was canceled.'
body: <p>So, I have been watching and learning .net core for few days now. I have built functioning API (with swagger)
  There is controller which I do use for now, which corresponds with my problem (Doubt there is problem with it, but to be complete):</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using BrambiShop.API.Data;
  using BrambiShop.API.Models;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  
  namespace BrambiShop.API.Controllers
  {
      [Route("api/[controller]")]
      [ApiController]
      public class CategoriesController : ControllerBase
      {
          private BrambiContext _context;
  
          public CategoriesController(BrambiContext context)
          {
              _context = context;
          }
  
          // GET: api/ItemVariants
          [HttpGet]
          public async Task&lt;IEnumerable&lt;Category&gt;&gt; GetAsync()
          {
              return await _context.Categories.ToListAsync();
          }
  
          // GET: api/ItemVariants/5
          [HttpGet("{id}")]
          public async Task&lt;Category&gt; GetAsync(int id)
          {
              return await _context.Categories.FindAsync(id);
          }
  
          // POST-add: api/ItemVariants
          [HttpPost]
          public async Task&lt;IActionResult&gt; PostAsync([FromBody] Category item)
          {
              if (!ModelState.IsValid)
              {
                  return BadRequest(ModelState);
              }
              _context.Categories.Add(item);
              await _context.SaveChangesAsync();
              return Ok();
          }
  
          // PUT-update: api/ItemVariants/5
          [HttpPut("{id}")]
          public async Task&lt;IActionResult&gt; PutAsync(int id, [FromBody] Category item)
          {
              if (!_context.Categories.Any(x =&gt; x.Id == id))
                  return NotFound();
  
              if (!ModelState.IsValid)
              {
                  return BadRequest(ModelState);
              }
              _context.Categories.Update(item);
              await _context.SaveChangesAsync();
              return Ok();
          }
  
          // DELETE: api/ItemVariants/5
          [HttpDelete("{id}")]
          public async Task&lt;IActionResult&gt; DeleteAsync(int id)
          {
              var itemToDelete = _context.Categories.Find(id);
              if (itemToDelete != null)
              {
                  _context.Categories.Remove(itemToDelete);
                  await _context.SaveChangesAsync();
                  return Ok();
              }
              return NoContent();
          }
      }
  }
  </code></pre>
  
  <p>Ok so, where is my problem. My problem lies in this method:</p>
  
  <pre><code>    public async void OnGet()
      {
          Categories = await _Client.GetCategoriesAsync();
      }
  </code></pre>
  
  <p>Which lies in my index.cshtml.cs.</p>
  
  <p>The GetCategoriesAsync itself:</p>
  
  <pre><code>using BrambiShop.API.Models;
  using System.Collections.Generic;
  using System.Net.Http;
  using System.Threading.Tasks;
  
  namespace BrambiShop.UI.Services
  {
      public interface IApiClient
      {
          Task&lt;List&lt;BrambiShop.API.Models.Category&gt;&gt; GetCategoriesAsync();
      }
  
      public class ApiClient : IApiClient
      {
          private readonly HttpClient _HttpClient;
  
          public ApiClient(HttpClient httpClient)
          {
              _HttpClient = httpClient;
          }
  
          public async Task&lt;List&lt;Category&gt;&gt; GetCategoriesAsync()
          {
              var response = await _HttpClient.GetAsync("/api/Categories");
              return await response.Content.ReadAsJsonAsync&lt;List&lt;Category&gt;&gt;();
          }
      }
  }
  </code></pre>
  
  <p>Thats is where I get the TaskCanceled exception. I have no clue, what is wrong here. It doesnt make any sense to me.
  Startup.cs defining HttpClient</p>
  
  <pre><code>            services.AddScoped(_ =&gt;
              new HttpClient
              {
                  BaseAddress = new Uri(Configuration["serviceUrl"]),
                  Timeout = TimeSpan.FromHours(1)
              });
              services.AddScoped&lt;IApiClient, ApiClient&gt;();
  </code></pre>
  
  <p>And this is the ReadAsJsonAsync method</p>
  
  <pre><code>using Newtonsoft.Json;
  using System.IO;
  using System.Net.Http;
  using System.Threading.Tasks;
  
  namespace BrambiShop.UI
  {
      public static class HttpClientExtensions
      {
          private static readonly JsonSerializer _jsonSerializer = new JsonSerializer();
  
          public static async Task&lt;T&gt; ReadAsJsonAsync&lt;T&gt;(this HttpContent httpContent)
          {
              using (var stream = await httpContent.ReadAsStreamAsync())
              {
                  var jsonReader = new JsonTextReader(new StreamReader(stream));
  
                  return _jsonSerializer.Deserialize&lt;T&gt;(jsonReader);
              }
          }
  
          public static Task&lt;HttpResponseMessage&gt; PostJsonAsync&lt;T&gt;(this HttpClient client, string url, T value)
          {
              return SendJsonAsync&lt;T&gt;(client, HttpMethod.Post, url, value);
          }
  
          public static Task&lt;HttpResponseMessage&gt; PutJsonAsync&lt;T&gt;(this HttpClient client, string url, T value)
          {
              return SendJsonAsync&lt;T&gt;(client, HttpMethod.Put, url, value);
          }
  
          public static Task&lt;HttpResponseMessage&gt; SendJsonAsync&lt;T&gt;(this HttpClient client, HttpMethod method, string url, T value)
          {
              var stream = new MemoryStream();
              var jsonWriter = new JsonTextWriter(new StreamWriter(stream));
  
              _jsonSerializer.Serialize(jsonWriter, value);
  
              jsonWriter.Flush();
  
              stream.Position = 0;
  
              var request = new HttpRequestMessage(method, url)
              {
                  Content = new StreamContent(stream)
              };
  
              request.Content.Headers.TryAddWithoutValidation("Content-Type", "application/json");
  
              return client.SendAsync(request);
          }
      }
  }
  </code></pre>
  
  <p>It all just comes to this error:
  <a href="https://i.stack.imgur.com/2PcNH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2PcNH.png" alt="enter image description here"></a></p>
  
  <p>Do anybody actualy know what is wrong and can maybe direct me the right way? I hope so, I have been unable to solve this for past 4 hours.</p>
  
  <p>Sincerely thanks.</p>
  
  <p>__</p>
  
  <p>I should also mention that sometimes it loads, and when I do something like</p>
  
  <pre><code>Debug.WriteLine(Categories.Count);
  </code></pre>
  
  <p>It gives me the right count, so data are loaded</p>
  
  <p>(also with writing out Names with foreach)</p>