azure 是否可以在单个foreach循环中调用多个活动触发器?[duplicate]

jgwigjjp  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(75)
    • 此问题在此处已有答案**:

What is the proper way to await for async inside of a ForEach?(3个答案)
5天前关闭。
我想为我的azure函数应用重复调用一些活动触发器,而这些触发器相互依赖。有没有办法在单个foreach循环中调用它们,或者我必须为整个数据列表分别迭代每个活动触发器,然后只有我才能继续下一个活动触发器?
下面是我在Orchestrator中使用的代码。

List<processAssignmentResult> assignmentResList = new List<processAssignmentResult>();

specialReqList.ForEach(async specialReq =>
{
  var actionStatus = await context.CallActivityAsync<ActionModel>("getRequestStatus", specialReq);

  var assignmentResult = await context.CallActivityAsync<processAssignmentResult>("processAssignment", actionStatus);
 assignmentResList.Add(assignmentResult);
});

bool sendAlert = await context.CallActivityAsync<bool>("sendAlertPostAssignment", assignmentResList);

ForEach循环外部的活动触发器(sendAlertPostAssignment)在循环过程完成之前被调用。

mwngjboj

mwngjboj1#

试试这个:

List<Task> tasks = new List<Task>();

    foreach(var specialReq in specialReqList)
    {
        var task = Task.Factory.StartNew(() => {
            var actionStatus = await context.CallActivityAsync<ActionModel>("getRequestStatus", specialReq);

            var assignmentResult = await context.CallActivityAsync<processAssignmentResult>("processAssignment", actionStatus);
            assignmentResList.Add(assignmentResult);
        });

        tasks.Add(task);
    }

    Task.WhenAll(tasks);

    bool sendAlert = await context.CallActivityAsync<bool>("sendAlertPostAssignment", assignmentResList);

相关问题