Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

482
Views
System.ObjectDisposedException: The CancellationTokenSource has been disposed

I sent 2 and more requests together for rest service and after second response I get an error. Also locally all works great, but on the env I catch exception.

The operation was canceled.

The error acquired: System.ObjectDisposedException: The CancellationTokenSource has been disposed.

at System.Threading.CancellationTokenSource.ThrowObjectDisposedException()

Can anyone suggest something?

[NotNull]
[ItemNotNull]
private async Task<TResponse> PostAsync<TRequest, TResponse>([NotNull] string path, [NotNull] TRequest request, CancellationToken cancellationToken) where TRequest : class where TResponse : class {
    var logger = new LoggerConfiguration()
                 .MinimumLevel.Verbose()
                 .WriteTo.Console()
                 .CreateLogger();

    Console.WriteLine("client");
    var client = this.GetHttpClient();

    Console.WriteLine("content");
    using var content = this.CreateHttpContent(request);

    Console.WriteLine("response");
    try {
        using var response = await client.PostAsync(path, content, cancellationToken).ConfigureAwait(false);
        return await this.ParseResponseAsync<TResponse>(response, cancellationToken).ConfigureAwait(false);
    } catch (OperationCanceledException exception) {
        logger.Verbose(exception.Message);
        throw;
    } catch (Exception exception) {
        Console.WriteLine(exception);
        logger.Verbose(exception.Message);
        logger.Error(exception.StackTrace);
        logger.Fatal(exception.InnerException?.Message);
        throw;
    }
}

[NotNull]
private HttpClient GetHttpClient() {
    var client = new HttpClient { BaseAddress = this.m_baseAddress };
    var headers = client.DefaultRequestHeaders;
    headers.Accept.Clear();
    headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MEDIA_TYPE_JSON));
    headers.ConnectionClose = true;
    return client;
}

[NotNull]
private HttpContent CreateHttpContent<TRequest>([NotNull] TRequest request) where TRequest : class {
    if(request == null) throw new ArgumentNullException(nameof(request));
    var content = JsonSerializer.Serialize(request, s_serializerOptions);
    return new StringContent(content, Encoding.UTF8, MEDIA_TYPE_JSON);
}

public async Task<long> CreateEmployeeRequestAsync(EmployeeRequestCreateRequest request, CancellationToken cancellationToken = default) {
if(request == null) throw new ArgumentNullException(nameof(request));
const string path = "employeerequest";
return (await this.PostAsync<EmployeeRequestCreateRequest, EmployeeRequestCreateResponse>(path, request, cancellationToken).ConfigureAwait(false)).RequestID;
}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!