title: Task in getting canceled
body: <p>I this service grabs the token from two places and attaches the token in every HTTP request as a header. I don't know why but I'm getting this error. I'm handling async but still, this task is getting canceled. </p>
  
  <pre><code>public class TokenService : DelegatingHandler, IHostedService
      {
          public IConfiguration Configuration { get; }
          private Timer _timer;
          public IHttpClientFactory _clientFactory;
          protected HttpClient _client_SB;
          protected HttpClient _client_Opsman_SB;
          private readonly IServiceScopeFactory _scopeFactory;
          string connectionString = "";
  
          public TokenService(IConfiguration configuration, IHttpClientFactory clientFactory, IServiceScopeFactory scopeFactory)
          {
              Configuration = configuration;
              _clientFactory = clientFactory;
              _scopeFactory = scopeFactory;
              connectionString = Configuration.GetConnectionString("DefaultConnection");
              _client_SB = _clientFactory.CreateClient("TestEnv");
              _client_Opsman_SB = _clientFactory.CreateClient("OpsmanTestEnv");
          }
  
          public Task StartAsync(CancellationToken cancellationToken)
          {
              _timer = new Timer(GetAccessToken, null, 0, 3300000);
              _timer = new Timer(GetAccessTokenOpsMan, null, 0, 3300000);
              return Task.CompletedTask;
          }
  
          public Task StopAsync(CancellationToken cancellationToken)
          {
              //Timer does not have a stop. 
              _timer?.Change(Timeout.Infinite, 0);
              return Task.CompletedTask;
          }
  
          public async Task&lt;Token&gt; GetToken(Uri authenticationUrl, Dictionary&lt;string, string&gt; authenticationCredentials)
          {
              HttpClient client = new HttpClient();
              FormUrlEncodedContent content = new FormUrlEncodedContent(authenticationCredentials);
              HttpResponseMessage response = await client.PostAsync(authenticationUrl, content);
  
              if (response.StatusCode != System.Net.HttpStatusCode.OK)
              {
                  string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
                  throw new ApplicationException(message);
              }
  
              string responseString = await response.Content.ReadAsStringAsync();
              Token token = JsonConvert.DeserializeObject&lt;Token&gt;(responseString);
  
              return token;
          }
          private void GetAccessToken(object state)
          {
              Dictionary&lt;string, string&gt; authenticationCredentials_sb = Configuration.GetSection("TestEnvironment:Credentials").GetChildren().Select(x =&gt; new KeyValuePair&lt;string, string&gt;(x.Key, x.Value)).ToDictionary(x =&gt; x.Key, x =&gt; x.Value);
              Token token_sb = GetToken(new Uri(Configuration["TestEnvironment:URL"]), authenticationCredentials_sb).Result;
  
              _client_SB.DefaultRequestHeaders.Add("Authorization", $"Bearer {token_sb.AccessToken}");
          }
  
          private void GetAccessTokenOpsMan(object state)
          {
  
              Dictionary&lt;string, string&gt; authenticationCredentials_opsman_sb = Configuration.GetSection("OpsManTestEnvironment:Credentials").GetChildren().Select(x =&gt; new KeyValuePair&lt;string, string&gt;(x.Key, x.Value)).ToDictionary(x =&gt; x.Key, x =&gt; x.Value);
              Token opsman_token_sb = GetToken(new Uri(Configuration["OpsManTestEnvironment:URL"]), authenticationCredentials_opsman_sb).Result;
              _client_Opsman_SB.DefaultRequestHeaders.Add("Authorization", $"Bearer {opsman_token_sb.AccessToken}");
          }
      }
  </code></pre>
  
  <blockquote>
    <p>Unhandled Exception: System.AggregateException: One or more errors
    occurred. (A task was canceled.) --->
    System.Threading.Tasks.TaskCanceledException: A task was canceled.
    --- End of inner exception stack trace --- at System.Threading.Tasks.Task`1.GetResultCore(Boolean
    waitCompletionNotification) at
    ProjectEcho.Services.TestService.GetAccessTokenOpsMan(Object state) in
    c:\agent_Work09\73\s\Services\TestService.cs:line 73 at
    System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown --- at System.Threading.TimerQueueTimer.CallCallback() at
    System.Threading.TimerQueueTimer.Fire() at
    System.Threading.TimerQueue.FireNextTimers()
            }</p>
  </blockquote>