Loading XAML Workflows in WF4
Note: This blog post is written using the .NET framework 4.0 RC 1
Most of the time I used compiled workflows in Windows Workflow Foundation 4. Its nice and easy, you design the workflow, compile it and at runtime there is a .NET type you use to create and run workflows. The main drawback is that this approach isn’t very flexible, sometimes you want to be able to change your workflow definition at runtime or store it in a database so recompiling isn’t an option.
Fortunately we can also load a workflow from the XAML file itself and execute the resulting workflow activity. This is done using the ActivityXamlServices class that will let us load the XAML file and return an activity, to be exact it returns a DynamicActivity as a wrapper around your definition.
The simplest option is just to call Load() passing in the file name like this:
Activity workflow = ActivityXamlServices.Load("YourWorkflow.xaml");
If you are using activities, or other types, from the local assembly this is going to fail though and you need a slightly more verbose way of doing thing like this:
var settings = new XamlXmlReaderSettings()
LocalAssembly = typeof(SendForManualApproval).Assembly
var reader = new XamlXmlReader("YourWorkflow.xaml", settings);
Activity workflow = ActivityXamlServices.Load(reader);
We need to use the XamlXmlReaderSettings to indicate what the local assembly reference in the XAML is.