Viewing a Workflow's Definition in Xoml

I attended a "get to know your son's/daughter's teacher" event last night for my son's new school (new in that it is still under construction). A major theme I took away is that teaching styles have drastically changed in the little bit of time I've been out of school (ok, maybe not little but it hasn't been looong). One thing I constantly heard was the goal of getting the students to think logically and derive their own conclusions. In order to keep the thinking on track, the teacher occasionally throws out little bits of information to get different gears in their heads rolling. Where is this going? This post is meant to throw a little curveball to get you thinking down another avenue of Windows Workflow Foundation that you might haven't seen yet.

When you create a new workflow project using the New Project Wizard you receive a nice project template consisting of a designer generated source file and a code behind file that you use whenever your workflow needs some additional logic (e.g. the CodeActivity needs a method for its ExecuteCode property). Did you know that your designer generated code can also be in the form of a Xml file? This Xml file is known as a xoml file. If you want to see what one of these looks like, right click on your project in Solution Explorer, select Add, New Item and select any item template that mentions something about "with code seperation", e.g. Sequential Workflow (with code seperation). What you get is a workflow definition expressed in xoml.

So now you're saying "oh man, that is cool. But I have this workflow I've been working on that I know would be painful to convert". Let's run a little experiment then, ok.

In the code that starts the WorkflowRuntime, adds any necessary services and then kicks off your workflow (i.e. in your workflow hosting code), let's add just a few lines. Follow these steps:

  1. Add 2 using statements for System.Workflow.ComponentModel.Serialization and for System.IO
  2. Add the following code after the call to StartWorkflow (I suggest you add it right after that call otherwise your workflow may finish before you get a chance to serialize it)

WorkflowMarkupSerializer ser = new WorkflowMarkupSerializer();

TextWriter tw = new StreamWriter(@"C:\myWorkflow.xoml");

ser.Serialize(workflowInstance.GetWorkflowDefinition(), tw);


Once you run your workflow, take a look at the myWorkflow.xoml file and voila! Now you'll have to do a little massaging on this file if you actually wanted to go ahead and create a new workflow project with it (tip: create a new sequential workflow with code seperation and follow what it does). Have fun exploring this awesome new technology and as always, don't hestitate to contact me.