Stanley.Goldman
Stanley.Goldman

Reputation: 4337

Using CallExternalMethodActivity/HandleExternalEventActivity in StateMachine

I'm attempting to make a StateMachine execute some database action between states.

So I have a "starting" state that uses CallExternalMethodActivity to call a "BeginExecuteNonQuery" function on an class decorated with ExternalDataExchangeAttribute. After that it uses a SetStateActivity to change to an "ending" state.

The "ending" state uses a HandleExternalEventActivity to listen to a "EndExecuteNonQuery" event.

I can step through the local service, into the "BeginExecuteNonQuery" function.

The problem is that the "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

Upvotes: 2

Views: 885

Answers (2)

Larry
Larry

Reputation: 18041

I'm using ExternalDataExchangeService without code by changing my config file as shown here :

Upvotes: 0

Maurice
Maurice

Reputation: 27632

I can't tell from the code snippet but make sure you are using the ExternalDataExchangeService to host your service and don't add you service directly to the runtime. The ExternalDataExchangeService is responsible for adding the required eventhandlers and turning the events into queued messages for the workflow.

Upvotes: 1

Related Questions