如果基于不断变化的值,Azure 逻辑应用程序条件不会在循环中工作

Azure Logic App Condition does not work in loop if based on changing values

我需要编写一个连接到 http 端点的简单 LogicApp,接收一些 JSON,遍历 JSON 消息并根据值将其提交到块中的不同 http 端点在消息中。

在这样做的过程中,我得出的结论是 For Each 循环中的条件总是评估循环之前的条件并执行与循环之前的结果匹配的路径,即使结果应该随着一些变化而改变变量已在循环中更新。

我已经设法用下面的例子说明了这个问题。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@variables('TestStr')",
                "runAfter": {
                    "Compose_3": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Compose_3": {
                "inputs": "@variables('TestArray')",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Compose_2": {
                        "inputs": "@variables('TestArray')",
                        "runAfter": {
                            "Condition": [
                                "Succeeded"
                            ]
                        },
                        "type": "Compose"
                    },
                    "Condition": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "TestArray",
                                    "value": "@items('For_each')"
                                },
                                "runAfter": {},
                                "type": "AppendToArrayVariable"
                            },
                            "Set_variable_2": {
                                "inputs": {
                                    "name": "TestStr",
                                    "value": "XXXX"
                                },
                                "runAfter": {
                                    "Append_to_array_variable": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "SetVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "Set_variable": {
                                    "inputs": {
                                        "name": "TestStr",
                                        "value": "not"
                                    },
                                    "runAfter": {},
                                    "type": "SetVariable"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@variables('TestStr')",
                                        "BlankValue"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@variables('FullArray')",
                "runAfter": {
                    "Initialize_variable_3": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "TestStr",
                            "type": "String",
                            "value": "BlankValue"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "TestArray",
                            "type": "Array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_3": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FullArray",
                            "type": "Array",
                            "value": [
                                {
                                    "key": "value1"
                                },
                                {
                                    "key": "value2"
                                },
                                {
                                    "key": "value3"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "recurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    }
}

我希望下面的 LogicApp 执行循环 3 次,每次评估条件并只执行一次数组插入,TestArray 包含

的一个条目
{
  "key":"value1"
}

和字符串 TestStr 的值为 'not'

但实际结果似乎有所不同 - TestArray 包含 FullArray 中的所有三个条目,而 TestStr 是 'XXXX'

我在这里错过了什么?有什么解决方法吗?

我发现了这种行为的原因。如果将来有人遇到此问题,请在此处发布。

For_each 循环默认并行执行。这就是为什么在循环开始之前对所有迭代评估条件的原因。

有一种方法可以关闭它 - 在 Settings you can switch on concurrency control and set parallelism 到 1