Monday, March 26, 2012

Working Workflow into your app's. Workflow Studio, a source available example of rehosting the Workflow Designer

Microsoft Developer Network - Samples - Workflow Studio


Rehosting the WF designer in an application outside of Visual Studio is nothing new and since WF4, nothing particularly difficult. In fact the WF product team has gone out of its way to make rehosting the designer as easy an experience as possible and they’ve done a great job. The WCF and WF Samples for .NET Framework 4 provides code samples for rehosting the designer with its corresponding toolbox and properties grid. The samples also cover handling validation errors, executing workflows and providing some level of debugging through workflow tracking – most of what you need to write your own rehosted designer.

But to get a useable app you’re going to have to write some boiler plate code to stitch all these concepts together. You’ll need code to open, save and execute workflows. You’ll need to manage window layout, display workflow output and handle runtime exceptions. And wouldn't it be nice to support working with multiple workflows at the same time all in a windowing environment that supports docking and pinning?

Enter Workflow Studio – a simple, generic application that allows you to design and execute multiple XAML based workflows in a windowed environment akin with Visual Studio. Use it to design and test your workflows in environments where Visual Studio is not the right tool. You can even use it as a simple hosting environment. I’ve developed Workflow Studio as an application you can use out of the box, or you can use it as the basis for your own specialised implementation.


MCS UK Solution Development Team - Workflow Foundation (WF4) – Rehosting The Workflow Designer



The following image shows the Workflow Studio environment:


Here’s a rundown of its features and where appropriate I've provided references to sample code should you want to understand more about the implementation detail.

  1. Develop XAML Based Workflows And Workflow Services
    Use the fully fledged WF designer to develop XAML based workflows and workflow services just as you would in Visual Studio. Interaction between the toolbox and properties box is just as in Visual Studio. For workflow services, add any WCF configuration to the Workflow Studio app config file. The solution comes with a test workflow service and client with appropriate configuration in the app config file as an example. Look at the Designer Rehosting example in WCF and WF Samples for .NET Framework 4 that demonstrates the basis for this implementation. Also, Pro WF Windows Workflow in .NET 4 by Bruce Bukovics has a great chapter on designer rehosting. Should you wish to execute other XAML based child workflows from a workflow then you can use the ExecuteXamlWorkflow custom activity I described in my previous post.
  2. Work On More Than One Workflow At The Same Time
    Workflow Studio allows you to develop multiple workflows in a single application.
  3. Toolbox Support For All Standard WF Activities
    All standard WF 4.0 activities are supported.
  4. Add Custom Activities To The Toolbox
    To add custom activities to the toolbox, copy the custom activity DLL to the bin folder of Workflow Studio, select the "Add Reference …" option and locate the DLL file. The custom activity will then become available in the toolbox. If your workflow references custom activities then these will be automatically loaded and added to the toolbox when the workflow is loaded providing the referenced DLL has been previously copied to the bin folder. The section on designer rehosting in Pro WF Windows Workflow in .NET 4 by Bruce Bukovics, providing the details of toolbox manipulation that I used as basis for Workflow Studio.
  5. Execute Workflows Concurrently
    You can execute one or more workflows or workflow services concurrently by either selecting "Start Debugging" or "Start Without Debugging" from the debug menu. Running workflows can be stopped by selecting "Abort" from the same menu. Each workflow has its own independent output window where WriteLine activity or exception stack trace output is written. You can also capture output through a trace source and associate a listener to log the output. We're using standard .NET diagnostics here, so it's totally flexible.
  6. Show Workflow Validation Errors
    Any validation errors detected by the designer are displayed in the error window along with error code and severity. Each workflow has its own independent window.
  7. Debug Workflows
    This isn't fully fledged Visual Studio debugging - it's workflow tracking. You'll see the currently executing activity highlighted in the designer so you'll be able to visually track the execution path of your workflow in real time. As each activity executes the activity name, id, state and workflow instance id are written to the workflow's debug window. Clicking on a row in the debug window will focus the designer on the corresponding activity. Similar to workflow output, the debug window output is also captured through a trace source so that output can be logged. Check out Visual Workflow Tracking in WCF and WF Samples for .NET Framework 4 which demonstrates how to implement visual tracking in the designer.
  8. Visual Studio Like Window Docking And Pinning
    One of the key features of Workflow Studio is to offer full window docking and pinning functionality that you find in Visual Studio. This functionality is provided by the AvalonDock open source WPF docking framework. This is a great framework but took some jumping through hoops to get working. I started out wanting to make this a pure MVVM application but soon discovered this wasn't easy, if at all possible. Apparently this is addressed with the up and coming 2.0 release along with other improvements. Note that you'll need to download and install AvalonDock independently of Workflow Studio since I don't redistribute it here - don't worry this is simple. Unfortunately there's no NuGet package available as yet so please follow instructions below.


Installing AvalonDock

As previously mentioned, AvalonDock is not distributed with Workflow Studio so you need to download and install it first. This version of Workflow Studio has been developed with version 1.3.3571 of AvalonDock so please ensure you install this version. The steps are simple:

  1. Download AvalonDock version 1.3.3571 from
  2. Run the installer. This will GAC the AvalonDock assembly.
  3. Build the solution. You should not need to modify any references.


If you've ever wondered at embedding the WorkFlow Designer into your app's (for example, like you see in TFS 2010 Team Build, etc) this is a project you should check out. This is not a binary project, you get the entire source to this. That's cool. Once I installed AvalonDock as mentioned above, the code just compiled and worked, no issues or problems


The Hello World Example (which you can run and debug right there in the Studio, now that's awesome).


Simply put, if you're thinking about Windows Workflow you should check this project out...

No comments: