Home > Enterprise >  Starting an Elsa workflow from a Workflow model in a controller
Starting an Elsa workflow from a Workflow model in a controller

Time:01-06

I am planning to use Elsa workflow for an enterprise application with acceptance flow (State machine). I am not sure about this process: I have multiple workflow models designed by admin user. Each is related to a different entity. Now in the corresponding api controller I want to start a workflow for the entity. The question is which libraries to inject to controller and use to create and instance and get the created instance id?

i.e.

public async Task<IActionResult> StartFlowOfSomeEntity(int entityID)
{
   //Is it the right way to load registry?
   var registeryList = await _workflowRegistry.ListAsync(CancellationToken.None);
   var relatedRegistery = registeryList.FirstOrDefault(uu => uu.Name == "MyFlowName");
   //Now need to create new instance?   
}

I am using UpdateInputAsync of WorkflowStorageService to apply outputs to the workflow instance.

I tried to use IWorkflowInstanceStore as sugested here but could not achieve the goal

CodePudding user response:

WorkflowRegistery is the right way to load models. But I prefer to use another method of it:

var relatedRegistery = await _workflowRegistry.FindByNameAsync("MyFlowName", VersionOptions.Latest);

For creating workflow instances you should inject WorkflowStarter to your controller and then do this:

RunWorkflowResult workflowInstance = await _wfStarter.StartWorkflowAsync(relatedRegistery , null, null, null, entityID.ToString());

Then workflowInstance.WorkflowInstance.Id has the value of the instance id you want.

You can also see this code at github doing similar thing with Elsa.

  • Related