How to: Serialize Workflows 

The Windows Workflow Foundation framework provides a serialization infrastructure that is used to serialize and deserialize a workflow. By default, a workflow is serialized to workflow markup according to certain formatting rules.

Default Serialization

Developers of new activities automatically get default serialization to workflow markup. This default serialization should be sufficient for most activities, but sometimes a custom serializer might be needed.

The following is an example of how to use the WorkflowMarkupSerializer class to serialize a workflow.

using System.Xml;
using System.IO;
using System.Workflow.ComponentModel.Serialization;
using System.Globalization;
...
// In the implementation of your host application, create a sequential 
// workflow.
SequentialWorkflowActivity workflow1 = new SequentialWorkflowActivity();

// Construct workflow.
...

// Serialize workflow.
WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
StringWriter strWriter = new StringWriter(CultureInfo.InvariantCulture);
XmlWriter xmlWriter = XmlWriter.Create(strWriter);
serializer.Serialize(xmlWriter, workflow1);

// Create a log file, open it, and write the serialized workflow to it.
File.WriteAllText(@"C:\logfile.txt", strWriter.ToString());
Imports System.Xml
Imports System.IO
Imports System.Workflow.ComponentModel.Serialization
Imports System.Globalization
...
' In the implementation of your host application, create a sequential 
' workflow.
Dim workflow1 as New SequentialWorkflowActivity()

' Construct workflow.
...

' Serialize workflow.
Dim serializer as new WorkflowMarkupSerializer()
Dim strWriter as new StringWriter(CultureInfo.InvariantCulture)
Dim xmlWriter as XmlWriter = XmlWriter.Create(strWriter)
serializer.Serialize(xmlWriter, workflow1)

' Create a log file, open it, and write the serialized workflow to it.
File.WriteAllText("C:\logfile.txt", strWriter.ToString())

Serialization Infrastructure

The Windows Workflow Foundation default serialization infrastructure provides all the types required to serialize the default activities. For information about the Windows Workflow Foundation framework default activity set, see Windows Workflow Foundation Activities.

The following are the important classes in the serialization infrastructure:

  • The WorkflowMarkupSerializer class is the base serialization type used in the serialization infrastructure. This type provides some of the basic services for serialization that implement the serialization rules. The serializers for activities or any other custom serializers for custom activities must inherit from this class.

  • The ActivityMarkupSerializer class inherits from the WorkflowMarkupSerializer class. This type is used to serialize all basic activities. These are activities that are not composite activities.

  • The CompositeActivityMarkupSerializer class inherits from the ActivityMarkupSerializer class and provides the serialization for composite activities. CompositeActivityMarkupSerializer adds more methods to process child activities, which can be serialized using their own serialization providers.

Serialization Format Rules

Workflow markup is the serialized form of object hierarchies. How an object is serialized depends on the object's properties. There are two main rules that help in the serialization of type instances:

  • The object's type definition is the element name in XAML.

  • The object's properties map to the attributes on the element definition in XAML.

The following code example illustrates an object serialization.

public Class ExampleActivity : Activity
 
    private string property1 = "SomeValue";
    private string property2 = "SomeOtherValue";

    public string ExampleProperty
    {
        get
        {
            return property1;
        }
        set
        {
            property1 = value;
        }
    }

    public string ExampleOtherProperty
    {
        get
        {
            return property2;
        }
        set
        {
            property2 = value;
        }
    }
// Activity.Execute and other overridden methods not included for clarity.
}

    
public class ExampleActivity
    Inherits Activity

    Private property1 As String = "SomeValue"
    Private property2 As String = "SomeOtherValue"

    Public Property ExampleProperty As String
        Get
            return property1
        End Get
        Set (ByVal value As String)
            property1 = value
        End Set
    End Property

    Public Property ExampleOtherProperty As String
        Get
            return property2
        End Get
        Set (ByVal value As String)
            property2 = value
        End Set
    End Property

' Activity.Execute and other overridden methods not included for 
' clarity.
End Class

An instance of this class is serialized as shown in the following XAML code example.

<ns0:ExampleActivity ExampleOtherProperty="SomeOtherValue" x:Name="ExampleActivity" ExampleProperty="SomeValue" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:CustomWFActivities;Assembly=CustomWFActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

The code example shows that the two string properties ExampleProperty and ExampleOtherProperty become attributes of the ExampleActivity element. The values "SomeValue" and "SomeOtherValue" are the actual values for the two fields.

Of course, a class definition can be much more complex than the code example's ExampleActivity class. Other rules apply in the serialization of more complex objects.

NoteNote

WorkflowMarkupSerializer does not support serialization of nested types. For example, if you create a custom activity and define a nested type within that activity, you will receive an error during compile time.

Custom Serialization

You can customize the serialization format. The customization can be done at two levels. A whole new serialization infrastructure can be provided, which includes a custom serialization format and a serialization for each type. The other serialization can be done at the level of activities. The serialization infrastructure is still workflow markup as provided by the Windows Workflow Foundation authoring framework, but the individual activity layout can be changed.

Customizing Activities

If you create a custom serializer, activity authors must provide a reference to the custom serializer as illustrated in the following code example.

[DesignerSerializer(typeof(ExampleActivityMarkupSerializer), typeof(WorkflowMarkupSerializer))]
public class ExampleActivity : Activity
{
}

[VB]

<DesignerSerializer(GetType(ExampleActivityMarkupSerializer), GetType(WorkflowMarkupSerializer))> _
Public Class ExampleActivity 
    Inherits Activity

End Class

The serializer is specified by using the DesignerSerializer attribute, which has two parameters:

  • The first parameter specifies the serializer that should be used to serialize the instance of the class on which the attribute has been defined.

  • The second parameter specifies the base type of the serializer. The base type of the serializer specifies the serialization scheme to use.

In the DesignerSerializer code example, the second attribute specifies WorkflowMarkupSerializer. This means that the base serialization infrastructure to be used is based on WorkflowMarkupSerializer.

Defining the Custom Serializer

The custom serializer is a class that must be inherited from a base serialization type. In the DesignerSerializer code example, ExampleActivityMarkupSerializer is a custom serializer for the ExampleActivity class. ExampleActivityMarkupSerializer inherits from WorkflowMarkupSerializer, which is the second parameter of the attribute.

When the serialization manager starts to serialize an activity, it uses the class definition to determine the custom serializer type by finding the base type of the WorkflowMarkupSerializer in the second parameter of the DesignerSerializer attribute. It then gets an instance of the serializer and uses it.

Workflow Markup

Workflow markup describes workflows that can be executed by the Windows Workflow Foundation runtime engine. Workflow markup is a component serialization scheme that is used to describe the activity hierarchies that make up workflows, and the associated logic that is activated when the activities raise events.

Workflow markup does not have any fixed grammar to describe it. It defines a general scheme that can be used to represent a hierarchy of objects, together with their properties and methods. Every activity has serialization logic that enables the activity metadata to be represented in markup that is defined for the object.

For more information, see Using Workflow Markup.

See Also

Reference

System.Workflow.ComponentModel.Serialization

Concepts

Serializing Custom Activities
How to: Compile Workflows
Using Workflow Markup

Other Resources

Developing Workflows

Footer image

Send comments about this topic to Microsoft.