Workflow Serialization Sample

Download sample

This sample demonstrates how to serialize a declarative workflow and how to deserialize and execute the workflow. A declarative workflow is created by using the workflow object model instead of in a standard code file.

In the Main method in Program.cs, a workflow instance is created programmatically, and a custom activity of type ConsoleActivity is added. Because no code-beside is available for declarative workflows, custom activities with overridden Execute methods must be used instead of Code activities with code handler routines. In the sample, the simple custom activity that is used takes in a string property called StringToWrite, and writes out that string to the console when executed.

Next, the workflow is serialized to a markup (XAML) file using WorkflowMarkupSerializer. The custom activity only has a property of type String. Therefore, you do not need a custom serializer for this workflow. For information about how to serialize activities that have complex property types, see the Custom Serialization Sample.

Next, a new workflow instance is created by deserializing the markup file. No deserializer is needed; instead, an override of the CreateWorkflow function is used. This override takes in an XmlReader object that points to a markup file.

By default, the CreateWorkflow method will also perform workflow validation, and a WorkflowValidationFailedException will be thrown if errors are found.

Finally, the workflow is started, and the StringToWrite property of the ConsoleActivity is written to the console.

By default, CreateWorkflow performs validation for every workflow instance that is created. If an application decides to optimize this behavior, and manage the validation by itself, the default validation can be disabled using the WorkflowRuntimeSection.ValidateOnCreate property. The following code can be used to validate the workflow instead.

// Get the type of the workflow and extract the validator attribute from it
Type workflowType = workflow.GetType();
ActivityValidatorAttribute validatorAttribute = (ActivityValidatorAttribute)workflowType.GetCustomAttributes(typeof(ActivityValidatorAttribute), true)[0];

// Load the validator type and create an instance of the validator
Type validatorType = Type.GetType(validatorAttribute.ValidatorTypeName);
Validator validator = (Validator)Activator.CreateInstance(validatorType);

// Create validation manager and validate the workflow
ValidationManager manager = new ValidationManager(workflowRuntime, true);
ValidationErrorCollection validationErrors = validator.Validate(manager, workflow);

Also in the case of the sample, since no custom types are used, a type provider is not needed to deserialize the workflow. If a type provider becomes necessary, it can be added using the following code:

// Push a type provider to resolve a referenced assembly
// The type provider is not necessary in this case, 
// since the referenced assembly is already loaded in the appdomain
// but it is shown for general purpose applications. 
TypeProvider typeProvider = new TypeProvider(null);

To build the sample

  1. Download the sample by clicking Download Sample.

    This extracts the sample project to your local hard disk.

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

  3. Go to the source directory of the sample.

  4. 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 HostApplication\bin\debug folder (or the HostApplication\bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also



Other Resources

Serializing Custom Activities
Serialization Overview
Workflow Markup Overview
Markup Samples

© 2007 Microsoft Corporation. All rights reserved.