How to Host a State Machine Workflow?

The following table details the procedure for hosting a state machine workflow. The code for hosting a state machine workflow is identical to that of a sequential workflow; however, starting the workflow may be different if you intend the workflow to respond to an event.

Step 1. Create a WorkflowRuntime object.

You can do this explicitly, where you create an instance of the runtime, or implicitly, in the same way as with a sequential workflow.

Step 2. Create a workflow instance.

You create the workflow instance object by using the WorkflowRuntime.CreateWorkflow method, which enables you to pass the type of workflow to create, and any input parameters. The method returns a WorkflowInstance object, which enables you to access properties that are specific to a particular instance.

Step 3. Raise events that trigger the workflow.

If you intend the workflow to start when an event is triggered, you create and raise the instance in the host process. The workflow runtime engine listens for the raised event and passes the event arguments to the workflow activity that is awaiting the event.

Step 4. Determine the current state of the workflow.

While the workflow is running, it is possible to retrieve the current state of the workflow from the StateMachineWorkflowInstance object.

You create a StateMachineWorkflowInstance object by passing the workflow runtime instance and the InstanceID workflow instance as parameters. The workflow runtime returns the workflow instance that corresponds to these values. You can then interrogate this instance object to retrieve the current state information.

Step 5. Handle events from the workflow.

To enable the workflow to handle events, you need to implement an ExternalDataExchange service, which enables the workflow runtime engine to pass data between the host process and the workflow instance. The exchange service interface determines the events that the workflow can respond to.

When the host process raises an event, the exchange service notifies the workflow runtime, which passes the event notification to the workflow as an inbound message.

The following code examples show how to host a state machine workflow. This code is slightly different from the code that is used to host a sequential workflow. The code examples create a WorkflowRuntime reference and then checks that the WorkflowRuntime object does not already exist before creating a new object. The code examples then assign handler methods to the events that the workflow exposes (the handlers are not shown in the examples) before creating an instance of the workflow and starting it.

// Declare a single WorkflowRuntime object that will host all

    // database workflow instances.

    WorkflowRuntime DbWorkflowRuntime = null;


    private void queryDatabase_Click(object sender, EventArgs e)


        // Create the database workflow runtime engine,

        // if it has not already been created.

        if (DbWorkflowRuntime == null)


            // Start the workflow runtime engine that will host

            // database workflow instances.

            DbWorkflowRuntime = new WorkflowRuntime();


            // Listen for workflow events.

            DbWorkflowRuntime.WorkflowCompleted += OnDbWorkflowCompleted;


            DbWorkflowRuntime.WorkflowTerminated += OnDbWorkflowTerminated;


            // Load the workflow type.

            Type type = typeof(ContosoMedicalWorkflows.DbWorkflow);


             // Create and start an instance of the workflow.

            WorkflowInstance instance = DbWorkflowRuntime.CreateWorkflow(type);









The following code examples show how to retrieve the current state of a state machine workflow. The code examples assume a reference to the workflow instance object, DbWorkflowInstance, and the workflow runtime object, DbWorkflowRuntime

// Get the state information for the workflow instance.

StateMachineWorkflowInstance instance =  

    new StateMachineWorkflowInstance(DbWorkflowRuntime,




// Display the current state of the workflow instance.

Console.WriteLine("Workflow instance {0}, current state: {1}",