c# - elsa 工作流 - 子工作流 - 如何获取父工作流的 workflowInstanceId

c# - elsa workflow - child workflow - how to get workflowInstanceId of parent workflow

我有一个 elsa 工作流 parent,它正在调用一些 child 工作流。 每个子工作流都有一些自定义活动(基于 SendHttpRequest elsa activity)。 这些自定义活动需要调用外部 api 并提供 parent 工作流的 workflowInstanceId

我试图将 parentworkflowInstanceId 作为子工作流的 ContextId 传递——不起作用,因为在子工作流自定义 activity 中被覆盖(这很有意义当然)。

我如何从子工作流中 运行 的一段代码中获取(服务器端)parent 工作流的 workflowInstanceId (假设来自 OnExecuteAsync(ActivityExecutionContext context) 的子工作流自定义 activity)?

尽管 Elsa 2(撰写本文时为 2.6)没有真正的 parent-child 工作流概念(即其他工作流调用的工作流没有对其父级的任何引用),您可以使用RunWorkflow activity 为子工作流提供任意 输入 。包括它自己的工作流实例 ID。

例如在RunWorkflowactivity上使用如下JS表达式将工作流实例发送给子工作流:

({ parentWorkflowInstanceId: workflowInstanceId })

然后,从您的子工作流中,您的第一个 activity 应该是捕获此值的那个,例如使用 SetVariable activity:

input.parentWorkflowInstanceId

现在您已将父工作流实例 ID 存储在工作流变量中,您可以从子工作流中的任何位置访问它。

这是您可以通过设计器导入的父工作流示例:

{
  "$id": "1",
  "definitionId": "f3da997090fe4752b4f7abf799f372a8",
  "versionId": "8d6c6e2b441a4d22952a7b19ebf9ceca",
  "name": "Parent",
  "version": 2,
  "variables": {
    "$id": "2",
    "data": {}
  },
  "customAttributes": {
    "$id": "3",
    "data": {}
  },
  "isSingleton": false,
  "persistenceBehavior": "WorkflowBurst",
  "deleteCompletedInstances": false,
  "isPublished": true,
  "isLatest": true,
  "activities": [
    {
      "$id": "4",
      "activityId": "2952daee-646e-431f-b02b-006557507755",
      "type": "RunWorkflow",
      "displayName": "Run Workflow",
      "persistWorkflow": false,
      "loadWorkflowContext": false,
      "saveWorkflowContext": false,
      "properties": [
        {
          "$id": "5",
          "name": "WorkflowDefinitionId",
          "syntax": "Liquid",
          "expressions": {
            "$id": "6",
            "Liquid": "{{ \"Child\" | workflow_definition_id: \"name\" }}"
          }
        },
        {
          "$id": "7",
          "name": "Input",
          "syntax": "JavaScript",
          "expressions": {
            "$id": "8",
            "JavaScript": "({ parentWorkflowInstanceId: workflowInstanceId})"
          }
        },
        {
          "$id": "9",
          "name": "PossibleOutcomes",
          "expressions": {
            "$id": "10"
          }
        },
        {
          "$id": "11",
          "name": "Mode",
          "expressions": {
            "$id": "12"
          }
        },
        {
          "$id": "13",
          "name": "TenantId",
          "expressions": {
            "$id": "14"
          }
        },
        {
          "$id": "15",
          "name": "CorrelationId",
          "expressions": {
            "$id": "16"
          }
        },
        {
          "$id": "17",
          "name": "ContextId",
          "expressions": {
            "$id": "18"
          }
        },
        {
          "$id": "19",
          "name": "CustomAttributes",
          "expressions": {
            "$id": "20"
          }
        }
      ],
      "propertyStorageProviders": {}
    },
    {
      "$id": "21",
      "activityId": "2a6d90ee-5e7a-4d68-8629-386abdaf70ad",
      "type": "HttpEndpoint",
      "displayName": "HTTP Endpoint",
      "persistWorkflow": false,
      "loadWorkflowContext": false,
      "saveWorkflowContext": false,
      "properties": [
        {
          "$id": "22",
          "name": "Path",
          "expressions": {
            "$id": "23",
            "Literal": "/parent"
          }
        },
        {
          "$id": "24",
          "name": "Methods",
          "expressions": {
            "$id": "25",
            "Json": "[\"GET\"]"
          }
        },
        {
          "$id": "26",
          "name": "ReadContent",
          "expressions": {
            "$id": "27"
          }
        },
        {
          "$id": "28",
          "name": "TargetType",
          "expressions": {
            "$id": "29"
          }
        },
        {
          "$id": "30",
          "name": "Schema",
          "syntax": "Literal",
          "expressions": {
            "$id": "31",
            "Literal": ""
          }
        },
        {
          "$id": "32",
          "name": "Authorize",
          "expressions": {
            "$id": "33"
          }
        },
        {
          "$id": "34",
          "name": "Policy",
          "expressions": {
            "$id": "35"
          }
        }
      ],
      "propertyStorageProviders": {}
    }
  ],
  "connections": [
    {
      "$id": "36",
      "sourceActivityId": "2a6d90ee-5e7a-4d68-8629-386abdaf70ad",
      "targetActivityId": "2952daee-646e-431f-b02b-006557507755",
      "outcome": "Done"
    }
  ],
  "id": "8d6c6e2b441a4d22952a7b19ebf9ceca"
}

这是您可以导入的 Child:

{
  "$id": "1",
  "definitionId": "81efcdb2f9aa473a84931814f5ee1c65",
  "versionId": "cc21182603564d68876efd9079382c27",
  "name": "Child",
  "version": 1,
  "variables": {
    "$id": "2",
    "data": {}
  },
  "customAttributes": {
    "$id": "3",
    "data": {}
  },
  "isSingleton": false,
  "persistenceBehavior": "WorkflowBurst",
  "deleteCompletedInstances": false,
  "isPublished": true,
  "isLatest": true,
  "activities": [
    {
      "$id": "4",
      "activityId": "83c6931f-f481-47c0-add8-3b1ea7d864a2",
      "type": "SetVariable",
      "displayName": "Set ParentWorkflowInstanceId Variable",
      "persistWorkflow": false,
      "loadWorkflowContext": false,
      "saveWorkflowContext": false,
      "properties": [
        {
          "$id": "5",
          "name": "VariableName",
          "expressions": {
            "$id": "6",
            "Literal": "ParentWorkflowInstanceId"
          }
        },
        {
          "$id": "7",
          "name": "Value",
          "syntax": "JavaScript",
          "expressions": {
            "$id": "8",
            "JavaScript": "input.parentWorkflowInstanceId"
          }
        }
      ],
      "propertyStorageProviders": {}
    },
    {
      "$id": "9",
      "activityId": "80e7eb8f-3fca-42a7-be3d-16827bd6ba38",
      "type": "WriteHttpResponse",
      "displayName": "HTTP Response",
      "persistWorkflow": false,
      "loadWorkflowContext": false,
      "saveWorkflowContext": false,
      "properties": [
        {
          "$id": "10",
          "name": "Content",
          "syntax": "Liquid",
          "expressions": {
            "$id": "11",
            "Liquid": "Parent ID: {{ Variables.ParentWorkflowInstanceId }}"
          }
        },
        {
          "$id": "12",
          "name": "ContentType",
          "expressions": {
            "$id": "13"
          }
        },
        {
          "$id": "14",
          "name": "StatusCode",
          "expressions": {
            "$id": "15"
          }
        },
        {
          "$id": "16",
          "name": "CharSet",
          "expressions": {
            "$id": "17"
          }
        },
        {
          "$id": "18",
          "name": "ResponseHeaders",
          "expressions": {
            "$id": "19"
          }
        }
      ],
      "propertyStorageProviders": {}
    }
  ],
  "connections": [
    {
      "$id": "20",
      "sourceActivityId": "83c6931f-f481-47c0-add8-3b1ea7d864a2",
      "targetActivityId": "80e7eb8f-3fca-42a7-be3d-16827bd6ba38",
      "outcome": "Done"
    }
  ],
  "id": "cc21182603564d68876efd9079382c27"
}

导入后,确保两个工作流都已发布。 然后,您可以通过其 HTTP 端点调用父工作流,例如:https://localhost:11000/parent

这将导致类似于此的响应:

Parent ID: 0178617bbeca47088e74330c0905237c

此响应由子工作流(成功接收到父工作流实例 ID)生成