Posted by: Cirilo Meggiolaro | 12/4/2008

Tip of the day #51 – Serializing Workflows (WWF)

The serialization of workflows created with Windows Workflow Foundation (WWF) is quite easy. The key to serialize a workflow is to use WorkflowMarkupSerializer class that provides functionalities to serialize and deserialize workflows to XAML. This class is used by the WWF workflow designer control to persist data to its file.

How to…

The following example creates an instance of a sequential workflow, adds a delay activity to it and then serializes the workflow instance using the WorkflowMarkupSerializer class. The rest is pretty straightforward.

/// Declares attributes
StringBuilder sbOutput = null;
XmlWriter writerXml = null;
WorkflowMarkupSerializer wkfSerializer = null;
SequentialWorkflowActivity wkfInstance = null;

try
{
    /// Creates a new sequential workflow
    wkfInstance = new SequentialWorkflowActivity();

    /// Creates a delay activity
    DelayActivity delay = new DelayActivity();

    /// Sets timeout to two minutes
    delay.TimeoutDuration = new TimeSpan(0, 2, 0);

    /// Adds the delay activity to the sequential workflow
    wkfInstance.Activities.Add(delay);

    /// Creates an instance of the string builder
/// that holds the output content
    sbOutput = new StringBuilder();

    /// Creates an instance of the XmlTextWriter.
    writerXml = XmlTextWriter.Create(sbOutput);

    /// Creates an instance of the Workflow Markup Serializer
    wkfSerializer = new WorkflowMarkupSerializer();

    /// Serializes the workflow instance
    wkfSerializer.Serialize(writerXml, wkfInstance);

    /// Sends output content to console.
    Console.WriteLine(sbOutput);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    /// Releases instances
    sbOutput = null;
    writerXml = null;
    wkfSerializer = null;

    if (wkfInstance != null)
    {
        wkfInstance.Dispose();
        wkfInstance = null;
    }
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: