【发布时间】:2021-09-11 14:44:18
【问题描述】:
我需要在 ForEach 循环中返回每个 API 调用的响应,而不是等待它结束。
第一个 Api 从 SharePoint 获取数据并调用另一个 Api 来更新网站数据库:
[HttpGet]
[Route("GetAllFaqs")]
public async Task<ApiResult<List<FaqModel>>> GetAllFaqs()
{
ApiSuccessResult<List<FaqModel>> faqs = await sharepointConnector.GetAllFaqsAsync();
foreach (var faq in faqs.Response)
{
// Here I want to return response of this Api call
// so I could show on client side one by one
await UpdateFaq(faq);
}
return faqs;
}
另一个更新网站数据库的 API:
[HttpPost]
[Route("UpdateFaq")]
public async Task<ApiResult<FaqSyncResult>> UpdateFaq(FaqModel model)
{
ApiSuccessResult<FaqSyncResult> result = await websiteConnector.SyncFaq(model);
return result;
}
是否可以在 ForEach 循环中返回响应而不停止它并且也无需等待结束?
【问题讨论】:
标签: c# api asynchronous .net-core