Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

484
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda