Recently somebody asked me how a workflow could invoke another workflow that is loaded as XAML from a file or a database. This is a common request so I’ve added an activity to Microsoft.Activities to support this.
Update: If you want tracking to include data from the child workflow be sure to update to Microsoft.Activities.1.8.3 or later. See Tracking Child Workflow with InvokeWorkflow for details
Update 2: 2011-09-26 The Sample has been updated to support loading of activity assemblies and setting the XamlXmlReader LocalAssembly
Download the Source for this example from MSDN Code Gallery – WF4 - How To Invoke a Child Workflow as XAML
For this simple example, I’ve create a child activity named Increment.xaml which accepts a number “Num” and increments it by one. This will be our child workflow.
The Parent Workflow is InvokeChild.xaml and it declares two variables StartingNumber (initialized to 3) and TheNumber which will be passed to the child workflow
For this working example, I’ve used the Increment activity and referenced it in the XAML at compile time. I want to have a good starting point for our example and as you can see it works.
In the latest release of Microsoft.Activities we have added several new activities to support this scenario
When I want to use Microsoft.Activities in a project I create a folder called Reference Assemblies, Download the latest release from http://wf.codeplex.com and add the assemblies to that folder. The I add a reference to Microsoft.Activities.dll.
Now I need to tell Visual Studio to treat Increment.xaml as content. To do this, in Solution Explorer, click on Increment.xaml and set the properties
Now I need to replace the Increment activity I added earlier with the LoadAndInvokeWorkflow activity. Sometimes Visual Studio doesn’t automatically add the activities from Microsoft.Activities to my toolbox. You can always add them manually if you need to.
No surprise here – it works when you run it if you did all the steps correctly.
One note – if you were going to run the activity hundreds of times you should not use LoadAndInvokeWorkflow but instead use LoadActivity to load it once and then InvokeWorkflow to invoke it over and over again.
Happy Coding! Ron Jacobs http://blogs.msdn.com/rjacobs Twitter: @ronljacobs http://twitter.com/ronljacobs