How to send data to a workflow

Sometimes you need to send data to a workflow from the host. To do this you need create an interface that is decorated with the ExternalDataExchange attribute and contains an event delegate like the following:

[ExternalDataExchange]

public interface IMyService

{

    event EventHandler<CompletedEventArgs> Completed;

}

In your workflow add a HandleExternalEventActivity and set its InterfaceType to the interface that contains the event delegate. Next select the EventName for the event that you want to handle. To raise the event from the host you need to implement the interface like the following:

public class MyService : IMyService

{

    public void RaiseEvent(CompletedEventArgs args)

    {

        EventHandler<CompletedEventArgs> completed = this.Completed;

        if (completed != null)

            completed(null, args);

    }

    public event EventHandler<CompletedEventArgs> Completed;

}

Then after creating the workflow runtime you need to add your custom service to the ExternalDataExchangeService and it needs to be added to the workflow runtime.

ExternalDataExchangeService dataExchange = new ExternalDataExchangeService();

workflowRuntime.AddService(dataExchange);

MyService myService = new MyService();

dataExchange.AddService(myService);

Below is a link to a simple sample that just traces out the string that is passed into the workflow.

SendDataToStateMachine.exe