Click here to Skip to main content
13,301,100 members (59,839 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 27 Jul 2012

Saving and loading Workflow Foundation 4 activities

, 27 Jul 2012
Rate this:
Please Sign up or sign in to vote.
All necessary bits and pieces to successfully save and load WF4 activities.


In one of my projects, I had the need for user editable workflows. The focus of this article is not how to enable the users to edit the workflows, i.e. how to host the designer. The focus of this article is the seemingly simple task of loading a XAML file that contains a WF4 activity definition in the format created by the Visual Studio designer and create just that format when saving the activity back to XAML.   

This proved harder than expected so I thought about sharing my results. Please note that this is my first article, so constructive criticism is always welcome.


The easiest way to load and save activities from and to XAML is to use the class ActivityXamlServices. A look at the documentation reveals no Save method but several Load methods, so lets start with the latter.  

Loading an activity from XAML

Loading the activity is actually very simple:

var activity = ActivityXamlServices.Load(reader);

reader is a TextReader, e.g. an instance of a StreamReader or StringReader.

As far as I am aware, this always returns an instance of type DynamicActivity when loading XAML that has been created by the VS designer or the Save method we are going to implement later.

For a round-trip of loading and saving it is important to have a DynamicActivity, the more general Activity doesn't help us. Because of this, I am trying to cast the result of Load to a DynamicActivity and throw an exception if that doesn't work:

var activity = ActivityXamlServices.Load(reader) as DynamicActivity;
if (activity == null)
    throw new InvalidDataException("The XAML doesn't represent a DynamicActivity.");

Saving a DynamicActivity to XAML

In order to get the same XAML that the VS designer creates, it is necessary to have an instance of ActivityBuilder. Depending on how you do it, saving a normal Activity either throws an exception or leads to XAML in a different format than the one we need. 

Creating an ActivityBuilder from a DynamicActivity

So, in order to save our activity to XAML we need to have an ActivityBuilder but all we do have is a DynamicActivity. The way from a DynamicActivity to an ActivityBuilder is actually very simple, as both classes have a very similar layout. We just new up an ActivityBuilder and assign its properties with the values of our DynamicActivity:

var activityBuilder = new ActivityBuilder();
activityBuilder.Implementation = dynamicActivity.Implementation != null ?
                                     dynamicActivity.Implementation() : null;
activityBuilder.Name = dynamicActivity.Name;
foreach (var item in dynamicActivity.Attributes)
foreach (var item in dynamicActivity.Constraints)
foreach (var item in dynamicActivity.Properties)
    var property = new DynamicActivityProperty
                       Name = item.Name,
                       Type = item.Type,
                       Value = null

    foreach (var attribute in item.Attributes)
VisualBasic.SetSettings(activityBuilder, VisualBasic.GetSettings(dynamicActivity));
return activityBuilder;  

This code is an extended version of code presented in Winfried Lötzsch's excellent article about rehosting the WF designer. For the most part, this is straight forward.

But my version of this code has two important differences. The first difference is this line:

VisualBasic.SetSettings(activityBuilder, VisualBasic.GetSettings(dynamicActivity));  

This line has two purposes:

  1. It creates a very important magic string in the XAML file:
        Assembly references and imported namespaces for internal implementation

    Without this, the activities our activity is composed of have no access to the input and output parameters of our workflow.

  2. It copies all the namespace imports from our DynamicActivity to the new ActivityBuilder.
    If this is omitted, the routine that we will later use to save the activity to XAML tries to infer the needed namespaces from the types used in the activity. It normally does a pretty good job at that task but it fails when it comes to extension methods that are used in expressions in the workflow.
    This leads to compiler errors when the XAML is later loaded and invoked.

The second difference is how I copy the properties:

foreach (var item in dynamicActivity.Properties)
    var property = new DynamicActivityProperty
                       Name = item.Name,
                       Type = item.Type,
                       Value = null

    foreach (var attribute in item.Attributes)

I am not simply adding the instances from the DynamicActivity but I am creating new instances of DynamicActivityProperty. For the most part, I assign the values of the old instance to new instance - with one important difference: I assign null to Value.

When an activity is loaded from XAML Value is null. When the activity is then executed, Value will be assigned by the WF engine. The problem now is that an assigned Value will lead to attributes on the root tag of the generated XAML. These attributes then lead to an exception when trying to load the XAML using the method described above.

That's it, now we have an ActivityBuilder instance we can save.

Saving the ActivityBuilder

Saving is not as straight forward as loading, but still not too hard:

var stringBuilder = new StringBuilder();
var xamlXmlWriter = new XamlXmlWriter(new StringWriter(stringBuilder), new XamlSchemaContext());
var builderWriter = ActivityXamlServices.CreateBuilderWriter(xamlXmlWriter);

XamlServices.Save(builderWriter, activityBuilder);
var xaml = stringBuilder.ToString(); 


Once you know what to look out for, it actually is pretty simple. The attached .cs file encapsulates all this functionality in a nice little helper class.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Daniel Hilgarth
Architect fire-development Ltd.
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionRe: Dynamic Activity Pin
Member 952046817-Oct-12 21:41
memberMember 952046817-Oct-12 21:41 
AnswerRe: Dynamic Activity Pin
Daniel Hilgarth17-Oct-12 22:00
memberDaniel Hilgarth17-Oct-12 22:00 
SuggestionNice summary Pin
Winfried Lötzsch28-Jul-12 7:26
memberWinfried Lötzsch28-Jul-12 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by Daniel Hilgarth
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid