[DOC] Blocking activities has incorrect code examples
Documentation Improvement Request
Documentation Issue Overview
I've been trying to implement the Workflows according to the documentation for Blocking activities.
- From my testing it seems the workflow will only be paused if "context.CreateBookmark();" is the final statement. I added a Console.WriteLine afterwards and the entire workflow ran - after removing it, it only ran up to this activity. Is my assumption here correct, and if so, is there a way to suspend in the middle of the activity?
- Also, the line "await workflowRunner.RunAsync(workflow, workflowState, options);" is not valid, since this particular RunAsync() overload doesn't seem to exist (anymore?). There is no RunAsync function where I could provide the workflowState and I can't find any other way of resuming the Workflow.
Would be neat if these things could be fixed - and someone mention here, which method I need to use to suspend/resume a workflow. :)
-
The behavior you're observing is consistent with the purpose of the
CreateBookmark()method. When you callcontext.CreateBookmark(), it indicates a point where the workflow should pause and wait for an external event to resume. If you place aConsole.WriteLineafterCreateBookmark(), it will execute immediately because the workflow has not yet been suspended. To suspend the workflow in the middle of an activity, you should ensure thatCreateBookmark()is the last call in the activity's execution path that should run before pausing. -
Regarding the
RunAsync()method, it seems there might be a discrepancy between the documentation and the actual API. The documentation you're referring to might be outdated or incorrect. To resume a workflow, you typically need to provide a bookmark ID to theRunAsync()method. If the overload you're looking for doesn't exist, you may need to use a different method or a different overload ofRunAsync()that is available in the version of Elsa you are using.
For the most accurate and up-to-date information on how to suspend and resume workflows, I recommend checking the latest official Elsa documentation or the source code directly. If the documentation is indeed outdated, you can open an issue on the Elsa GitHub repository or contact the maintainers to request an update.
For now, you can try using the WorkflowInstanceFilter to find the workflow instance and then use the RunWorkflowOptions to specify the bookmark ID when calling RunAsync(). Here's an example based on the information from the GitHub issues:
var options = new WorkflowInstanceFilter
{
Id = InstanceId,
};
var workflowInstance = await _workflowInstanceStore.FindAsync(options);
var workflowState = workflowInstance.WorkflowState;
var bookmark = workflowState.Bookmarks.FirstOrDefault(b => /* your condition here */);
var runOptions = new RunWorkflowOptions { BookmarkId = bookmark?.Id };
var result = await _workflowRunner.RunAsync(workflow, runOptions);
Please note that the above code is just an example and may need to be adjusted based on the actual API and your specific use case.
This comment was generated by Glime.
Right, in that case the first point can be ignored (since it was mostly a question anyway).
The second point still stands and using the RunAsync method like in the example will run the entire workflow, not just resume it.
Do you know the correct code? I'm trying to do this as well and running into trouble....
@douglasg14b their second doc worked for me: https://elsa-workflows.github.io/elsa-documentation/custom-activities.html?section=Programmatic#blocking-activities
The IWorkflowRunTime
workflowRuntime.TriggerWorkflowsAsync(activityTypeName, bookmarkPayload);
can execute the workflows again.
Only done some light testing so far, but it seems that the entire Activity is getting executed after all - instead of what's been discussed so far.