C#-ELSA工作流程 - 儿童工作流 - 如何获得父级工作流的工作流程

发布于 2025-01-19 13:54:17 字数 625 浏览 0 评论 0原文

我有一个elsa workflow parent,该调用某些child workflows。 每个子工作流程都有一些自定义活动(基于sendhttprequest elsa活动)。 这些自定义活动需要调用外部API并提供parent> parent> parent> parent工作流的workflowInstanceID

我尝试通过workflowinstanceid parent> parent作为condect> contextId of子工作流 - 由于儿童工作流的内部被覆盖,因此无法工作(这很有意义(这很有意义)当然)。

我如何获得(服务器端)workflowinstanceid parent> parent> parent从一个在子工作流程上运行的代码的工作流程 (假设来自onexecuteasync(ActivityExecutionContext上下文上下文) of Child Workflow自定义活动)?

I have an elsa workflow parent which is calling some child workflows.
Every child workflow has some custom activities (based on SendHttpRequest elsa activity).
These custom activities need to call an external api and provide the workflowInstanceId of parent workflow.

I tried to pass the workflowInstanceId of parent as ContextId of child workflow – not working since is overridden inside of child workflow custom activity (which makes sense of course).

How could I get (server-side) the workflowInstanceId of parent workflow from a piece of code which is running on a child workflow
(let’s say from OnExecuteAsync(ActivityExecutionContext context) of child workflow custom activity)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2025-01-26 13:54:17

尽管ELSA 2(写作时2.6)没有亲子工作流的真实概念(即其他工作流调用的工作流程没有任何参考),但您可以使用runworkflow活动为子工作流提供任意输入。包括其自己的工作流实例ID。

例如,使用runworkflow活动上的以下JS

({ parentWorkflowInstanceId: workflowInstanceId })

表达setVariable活动:

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"
}

这是您可以导入的孩子:

{
  "$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

此响应是由Child Workflow生成的(哪个)成功收到了父工作流实例ID)

Although Elsa 2 (2.6 at the time of writing) does not have a real notion of parent-child workflows (i.e. workflows invoked by other workflows do not have any reference to their parent), you can use the RunWorkflow activity to provide arbitrary input to the child workflow. Including its own workflow instance ID.

For example, use the following JS expression on the RunWorkflow activity to send the workflow instance to the child workflow:

({ parentWorkflowInstanceId: workflowInstanceId })

Then, from your child workflow, your first activity should be the one to capture this value, e.g. using a SetVariable activity:

input.parentWorkflowInstanceId

Now that you have the parent workflow instance ID stored in a workflow variable, you can access it from anywhere in your child workflow.

Here is an example of a parent workflow you can import via the designer:

{
  "$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"
}

And here is the Child you can import:

{
  "$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"
}

Once imported, make sure both workflows are published.
You can then invoke the parent workflow via its HTTP endpoint, e.g.: https://localhost:11000/parent

Which will result in a response similar to this:

Parent ID: 0178617bbeca47088e74330c0905237c

This response is generated by the Child workflow (which successfully received the parent workflow instance ID)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文