I am using web api 2, and entity framework 6. I have created an async web api, which updates all the records at once. I am also using Autofac for dependency injection.
My service interface is as follows :
Task<Approval> TakeAction(int id, bool isApprove)
void TakeAction(bool isApprove)
These are my service methods :
public async void TakeAction(bool isApprove)
{
//GetAllDataToApprove is the same function in the service.
var approvalList= GetAllDataToApprove().Approvals.ToList();
foreach (var approval in Approvals)
{
//This is an async method as well
await TakeAction(approval.ApprovalId, isApprove);
}
}
TakeAction method is as follows :
public async Task<Approval> TakeAction(int id, bool isApprove)
{
Approval approval= approvalrepository.Query(o => o.ApprovalId == id).FirstOrDefault();
try
{
//updating the approval status
approval.StatusId=5
UpdateDashboard(approval);
approvalrepository.Update(approval);
await unitOfWork.SaveChangesAsync();
}
}
catch (Exception ex)
{
throw ex;
}
return approval;
}
My web-API method is as follows :
[HttpPut]
public IHttpActionResult Put(bool isApprove)
{
try
{
approvalservice.TakeAction(isApprove);
return Ok();
}
catch (Exception ex)
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
}
}
I want to make a non-blocking API call, such that if this API is triggered, it should approve all the pending data. The reason I made this async is because there are lot of data in the pending list, so it takes a long time.
I am getting the following error: The operation cannot be completed because the DbContext has been disposed.