Using CallExternalMethodActivity / HandleExternalEventActivity in StateMachine
I am trying to make StateMachine perform some kind of database action between states.
So, I have an "initial" state that uses CallExternalMethodActivity to call the "BeginExecuteNonQuery" function in a class decorated with ExternalDataExchangeAttribute. After that, it uses SetStateActivity to enter the "finished" state.
The "End" state uses HandleExternalEventActivity to listen for the "EndExecuteNonQuery" event.
I can execute a local service in the "BeginExecuteNonQuery" function.
The problem is that "EndExecuteNonQuery" is null.
public class FailoverWorkflowController : IFailoverWorkflowController
{
private readonly WorkflowRuntime workflowRuntime;
private readonly FailoverWorkflowControlService failoverWorkflowControlService;
private readonly DatabaseControlService databaseControlService;
public FailoverWorkflowController()
{
workflowRuntime = new WorkflowRuntime();
workflowRuntime.WorkflowCompleted += workflowRuntime_WorkflowCompleted;
workflowRuntime.WorkflowTerminated += workflowRuntime_WorkflowTerminated;
ExternalDataExchangeService dataExchangeService = new ExternalDataExchangeService();
workflowRuntime.AddService(dataExchangeService);
databaseControlService = new DatabaseControlService();
workflowRuntime.AddService(databaseControlService);
workflowRuntime.StartRuntime();
}
...
}
...
public void BeginExecuteNonQuery(string command)
{
Guid workflowInstanceID = WorkflowEnvironment.WorkflowInstanceId;
ThreadPool.QueueUserWorkItem(delegate(object state)
{
try
{
int result = ExecuteNonQuery((string)state);
EndExecuteNonQuery(null, new ExecuteNonQueryResultEventArgs(workflowInstanceID, result));
}
catch (Exception exception)
{
EndExecuteNonQuery(null, new ExecuteNonQueryResultEventArgs(workflowInstanceID, exception));
}
}, command);
}
What am I doing wrong with my implementation?
-stan
source to share
I can't tell from the code snippet, but make sure you use the ExternalDataExchangeService to host your service and don't add your service directly at runtime. The ExternalDataExchangeService is responsible for adding the required event handlers and turning events into message queues for the workflow.
source to share