C# – InvalidOperationException and Workflows in ASP.NET MVC

asp.net-mvccnetworkflowworkflow-foundation

I'm trying to query a running state machine workflow using StateMachineWrokflowInstance in ASP.NET MVC.

Here is the scenario:

  1. Workflow runtime configuration: added SqlWorkflowPersistenceService, ManualWorkflowSchedulerService, ExternalDataExchangeService and registered custom ExternalDataExchange service with ExternalDataExchangeService;

  2. Execution sequence:

    var instance = WorkflowRuntimeHandle.CreateWorkflow(type);
    instance.Start();
    WorkflowRuntimeHandle.GetService<ManualWorkflowSchedulerService>
    ().RunWorkflow(instance.InstanceId);
    
    var stateMachineWorkflowInstance = new
    StateMachineWorkflowInstance(instance.WorkflowRuntime, instance.InstanceId); 
    
  3. Received error:

    System.InvalidOperationException: Workflow with id "[GUID]" not found in 
    state persistence store?
    

What am I doing wrong?

Best Answer

Apparently this exception could have many causes.

I found a way to detect the culprit. I added handlers for all the workflow runtime events and stored the sequence as history in a list and discovered from the list that after RunWorkflow is called the workflow was terminated.

The WorkflowTerminated event parameter WorkflowTerminatedEventArgs comes with a Exception property which includes inner exceptions that showed the real source of the problem.

I'm posting this here in hope that this experience would be beneficial to those who are reading.