Workflow Serialization


Workflow serialization is the process of persisting a workflow definition to XAML. Workflow serialization is similar to standard XML serialization of .NET objects; however, workflow serialization takes a workflow definition, no matter how it was developed, and writes that to an XAML file. Standard XML serialization of .NET classes is generally used to persist the state of a class instance rather than its definition. After a workflow has been serialized, the workflow runtime can execute it. The workflow namespaces provide several classes to assist in the serialization process.

So what is serialization used for? Well, in this author’s opinion, one of the most useful things that serialization provides is the ability to persist workflows created by end users. You can develop applications that allow end users to create workflows of their own and save them to a durable medium. This could be the filesystem or a database table. Because the output of the serialization process is plain XML, it is highly portable and flexible.

The following code example builds a workflow definition programmatically and then serializes to a .xoml file. First, an instance of the SequentialWorkflowActivity class that represents the workflow definition is created. Next, several child activities are created and added to the workflow or their respective parent activities. Finally, an instance of WorkflowMarkupSerializer is created and used to write the workflow definition that was just created to a file called myWorkflow.xoml.

  SequentialWorkflowActivity myWorkflow = new SequentialWorkflowActivity(); myWorkflow.Name = "myWorkflow"; ParallelActivity parallelActivity1 = new ParallelActivity(); SequenceActivity sequenceActivity1 = new SequenceActivity(); SequenceActivity sequenceActivity2 = new SequenceActivity(); parallelActivity1.Activities.Add(sequenceActivity1); parallelActivity1.Activities.Add(sequenceActivity2); CodeActivity codeActivity1 = new CodeActivity(); CodeActivity codeActivity2 = new CodeActivity(); sequenceActivity1.Activities.Add(codeActivity1); sequenceActivity2.Activities.Add(codeActivity2); myWorkflow.Activities.Add(parallelActivity1); WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer(); XmlWriter xmlWriter = XmlWriter.Create(@"C:\myWorkflow.xoml"); serializer.Serialize(xmlWriter, myWorkflow); 

This results in the following XML:

  <?xml version="1.0" encoding="utf-8"?> <SequentialWorkflowActivity   x:Name="myWorkflow"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">    <ParallelActivity x:Name="parallelActivity1">       <SequenceActivity x:Name="sequenceActivity1">          <CodeActivity x:Name="codeActivity1" />       </SequenceActivity>       <SequenceActivity x:Name="sequenceActivity2">          <CodeActivity x:Name="codeActivity2" />       </SequenceActivity>    </ParallelActivity> </SequentialWorkflowActivity> 

Serialization Classes

The major classes in the Windows Workflow Foundation serialization infrastructure are located in the System.Workflow.ComponentModel.Serialization namespace. They are as follows:

  • WorkflowMarkupSerializer - This is the base class for all serialization classes in Windows Workflow Foundation. You can use it to serialize workflows and activities to workflow markup XAML. In addition, you can deserialize workflow markup into corresponding workflow and activity objects.

  • ActivityMarkupSerializer - You use this class used to serialize the definition of non-composite workflow activities.

  • CompositeActivityMarkupSerializer - This class allows you to serialize more complex, composite activities. Composite activities act as containers for other activities.

Custom Serialization

You can specify which serializer should be used on a particular custom-developed activity. To do so, decorate the activity class with the DesignerSerializer attribute that exists in the System .ComponentModel.Design.Serialization namespace, as shown in the following code:

  [DesignerSerializer(typeof(MyCustomSerializer), typeof(WorkflowMarkupSerializer))] public class MyActivity : Activity {    ... } 

The first parameter of the DesignerSerializer attribute is a type reference to the serializer for this activity. The second parameter represents the first parameters base type that defines the serialization schema.



Professional Windows Workflow Foundation
Professional Windows Workflow Foundation
ISBN: 0470053860
EAN: 2147483647
Year: 2004
Pages: 118
Authors: Todd Kitta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net