We have a REST service developed in C# that internally calls various APIs. This service takes a while to complete due to multiple API calls. It aggregates the results from all the internal services and creates a dataset.
I only trigger the service but I don’t wait for its response; the service runs in the background.
The code structure is as follows:
try
{
/*Dozens of different APIs are being requested. As a result of these requests,
a data set is being created for the user.*/
}
catch (Exception ex)
{
requestDetail.IsError = true;
requestDetail.ErrorDetail = $"{ex.Message} ST: {ex.StackTrace}";
}
finally
{
requestDetail.IsComplated = true;
requestDetail.ComplatedDate = DateTime.Now;
//requestDetail model is being saved to MongoDB.
}
In the finally block, I update the IsCompleted field of the requestDetail model to true in MongoDB. For every operation that runs, I expect the IsCompleted field to be updated to true.
Within the service, I log every operation to MongoDB. Upon reviewing the service logs, most of the time everything operates as expected. However, for some operations, I find that the finally block does not execute. In different points, the operations are interrupted, and I cannot find any error logs. Somehow, the finally block isn’t being executed. The IsCompleted field does not appear to be updated to true.
I’m wondering if the thread is dying when the processes take too long.
How can I catch this if it is happening? How can I resolve this issue?