Udostępnij za pośrednictwem


Custom Serialization Sample

Download sample

This sample builds on the Workflow Serialization Sample sample. It demonstrates how to serialize workflows that contain activities that have properties that cannot be serialized by the default serializer.

In the sample, a declarative workflow is created. This is a workflow that is assembled using the Workflow object model instead of being defined in a code file. Two custom activities are added: QueueActivity and StackActivity, which have a property of type Queue and of type Stack respectively. Because Queue and Stack objects do not have the default Add and Remove methods that are common to most collection types, the standard workflow serializer does not know how to access their data. Therefore, custom serializers must be implemented for those types.

To implement the custom serializer, you must first override the default activity serializer. You do this with an attribute definition on the custom activity:

[DesignerSerializer(typeof(QueueActivitySerializer), typeof(WorkflowMarkupSerializer))]

The purpose of the custom activity serializers (QueueActivitySerializer and StackActivitySerializer) is to add and remove a custom serializer for the object type that is used by the parameter in question (in the sample, Queue and Stack) before and after serialization and deserialization. The custom activity serializers call AddSerializationProvider and RemoveSerializationProvider on the serialization manager that is exposed by the relevant events. (These are OnBeforeSerialize and OnAfterSerialize for serialization, and OnBeforeDeserialize and OnAfterDeserialize for deserialization.)

The object-type serializers (QueueSerializer and StackSerializer) override the relevant methods of the serializers' base class (WorkflowMarkupSerializer) to access the objects in the appropriate collection type. Therefore, the Queue serializer calls Queue.Enqueue to add objects to the queue, and the Stack serializer calls Stack.Push. Similarly, the GetChildren methods convert the collections to array lists. With the Stack object, this involves reversing the order of the items to provide the same first-in, last-out behavior of a stack.

Note

While creating workflows that use Queue and Stack objects is supported via the method described in this sample, using these collection types is not supported for workflows compiled at run time, as described in the Simple In-Memory Sample.

To build the sample

  1. Download the sample by clicking Download Sample.

  2. This extracts the sample project to your local hard disk.

  3. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  4. Go to the source directory of the sample.

  5. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the CustomSerialization\bin\debug folder (or the CustomSerialization\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also

Other Resources

Markup Samples
Workflow Markup Overview

© 2007 Microsoft Corporation. All rights reserved.