Udostępnij za pośrednictwem


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

Comments

  • Anonymous
    June 04, 2006
    Can I pass parameters as I need in the raise event method?

  • Anonymous
    February 08, 2007
    Mr Lake  This article is awesome! Please do write more & more...& I will frequent your blog! Thank You  moredotnet

  • Anonymous
    February 08, 2007
    Where do we write the class MyService, in the ASP.NET project or in the Workflow Project??? Please Help!

  • Anonymous
    April 28, 2008
    What is the naming convention for raising events? RaiseEvent is flagged by FxCop by UseEventsWhereAppropriate warning. Raise is no good either :-(