Sometimes I wonder... Other times I know.
Wednesday, February 22, 2006
 
Parameters into a workflow
We're in the end-game for Windows Workflow Foundation v1.0 and its great listen to the feedback some of our early adopters have given us.

Recently, a customer mentioned that it was hard to find samples of frequent developer patterns. I often see very similar questions on the same patterns posted to our internal discussion alias.

I'm going to try and increase the discoverability of these topics by posting a few blog entries which illustrate some of these common topics.

Note: You can find all of the WF samples in \Program Files\Microsoft SDKs\Windows Workflow Foundation\Samples.zip

The first topic is getting data into a workflow using parameters. I previously posted a blog entry on using Local Services which you might find useful as well.

For the purpose of this example, lets add a CodeActivity which sets the OutValue to the InValue plus some simple text


In this sample, we're going to pass a string into the workflow, slightly modify it, and return it back to the host.

Ok, to start, lets create a Sequential Workflow Console Application and modify the workflow first.

Parameters are defined as properties on the workflow class. Lets create an inbound parameter/property (InValue) and an outbound parameter/property (OutValue).



For the purpose of this example, lets add a CodeActivity which sets the OutValue to the InValue plus some simple text.

Here are the properties and the logic for the code handler.



Ok, at this point, we've finished with the workflow. It will receive the data into InValue, the Code activity will execute and set OutValue to InValue and the string "changed in the workflow."

Now lets move to the host which will set the InValue and and will receive the output. Data is passed into the workflow via a dictionary. Results from the workflow can be retrieved from an dictionary exposed through an event indicating that the workflow has completed. Lets drill into the code that shows this.

One of the overloads of the CreateInstance() method on WorkflowRuntime takes a Dictionary. The key needs to match the name of the property defined on the workflow, in this case InValue.



One of the events fired by workflowRuntime is OnWorkflowCompleted. First, lets setup an EventHandler which listens for this event. This event handler provides WorkflowCompletedEventArgs from which we can extract the output value from the OutputParameters dictionary.



Here's the code for this sample..