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;
}