I have a controller action, which gets a list of document types, then makes a webservice call for each document type. I want to make these all at once, so that looping over them only takes as long as the longest one. I don't know if my code is correct, and I need to do something else, or if my code is simply incorrect.
Action:
public ActionResult GetPlan(MemberViewModel request)
{
DocService ds = new DocService();
List<DocType> docTypes = ds.GetDocTypesForPlan(request.PlanId);
List<CoverageDocument> coverageDocuments = ds.GetDocumentsForDocTypes(docTypes);
return View(coverageDocuments);
}
GetDocumentsForDocTypes:
public List<CoverageDocument> GetDocumentsForDocTypes(List<DocType> planDocTypes)
{
List<CoverageDocument> planDocuments = new List<CoverageDocument>();
DocumentUtility documentUtility = new DocumentUtility();
int lastYear = DateTime.Now.Year - 1;
planDocTypes.ForEach(async (docType) =>
{
DocumentUtility.SearchCriteria sc = new DocumentUtility.SearchCriteria();
sc.documentType = docType;
Dictionary<long, Tuple<string, string>> documentList = await documentUtility.FindDocuments(sc);
documentList.ToList().ForEach((document) =>
{
CoverageDocument doc = this.coverageDocumentConstructor(document);
planDocuments.Add(doc);
});
});
return planDocuments;
}
Exception:
Additional information: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.