1

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;
}
Anthony14
  • 105
  • 6
  • 2
    It seems the problem is not in your posted code. Where and how do you use `PostAsync`? The CancellationTokenSource of `cancellationToken` gets disposed before PostAsync ends. – Steeeve Aug 18 '21 at 12:13
  • @Steeeve This method gets data from front-end and transmitted to PostAsync method And returns a request id from rest service. ( The code I added to question ) – Anthony14 Aug 18 '21 at 13:59
  • Hm... I don't have any experience with ASP.Net or Web Api, but after reading [Best practice to call ConfigureAwait for all server-side code](https://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code), maybe it has to do with `ConfigureAwait(false)`, depending on what framework you use. I would add the relevant tags to the question. – Steeeve Aug 18 '21 at 14:57

0 Answers0