Windows Workflow Foundation in .NET4

Windows Workflow Foundation (WF4) in .NET 4 is designed to make it easier for new developers to learn, addresses a wider range of customer scenarios, and is more efficient.

WF is a programming model for composing application logic and coordinating execution, allowing developers to abstract complicated code while leveraging a set of runtime services. Activities are the building blocks that are composed together to build workflows. The runtime provides the ability to save the state of the workflows, track information about the workflow, and manage the coordination of multiple workflows executing at the same time.

Design your Workflow

Visual Studio 2010 represents a significant update to the WF design experience. Performance and extensibility have been dramatically improved, and the designer surface has an improved, WPF-based look and feel.

The new designer has significant productivity enhancements for authors composing large workflows, including the ability to expand and collapse child activities, a breadcrumb trail along the top of the designer that allows users to drill in and out of deep workflows, and better visualization capabilities to discover and address validation errors.

Designer features 

The workflow designer in WF4 is a WPF control that you can use to host the designer within your custom applications with a few lines of code. This allows you to create a custom workflow-editing experience within your application and allows your users to visualize an executing workflow within your applications. 

Introducing Flowchart: The New Control Flow Style

WF4 introduces a flowchart style of control flow, which allows you to create flexible workflows that require the ability to loop back to previous steps, as well as skip steps based on conditions within the workflow. This is coupled with new tooling that allows for flowcharts to be constructed visually. The new flowchart control flow in WF4 allows for business logic to be composed in a way that feels more natural to the workflow author and in a way that is more visually compact. Below, the flowchart style is used to build a hiring process workflow.

Hiring process workflow

A Simplified Activity Model

At the heart of WF is the authoring of custom activities for use in higher-order workflows, and WF4 makes creating custom activities easier than ever. WF4 dramatically reduces the amount of code that developers need to write to implement custom application logic for a variety of scenarios. As the activity model has been simplified, the performance of the runtime has dramatically increased.

The way activities are authored has also been updated to provide options that are optimized for specific scenarios, supplying a variety of activity base classes that enable the developer to make the authoring experience only as powerful or complex as required. The activity model also supports asynchronous execution, allowing a workflow to compose and coordinate multiple concurrent branches of asynchronous logic. In addition, the model now includes an ActivityAction feature, which enables you to write an activity that can be customized by offering the consumer of your activity the ability to plug in type-safe callbacks for custom logic. This would, for example, allow you to create a ProcessOrder activity that allows the consumer to provide their implementation of HandlePayment.

Building a Workflow

To demonstrate WF, let’s construct a workflow to retrieve the syndication feeds from a number of blogs. Here we will build a workflow that will use a GetWebPage custom activity to retrieve a number of feeds in parallel. Additionally, we use the CompletionCondition of the ParallelForEach in order to stop retrieving feeds once half of the requests have completed. This lets my workflow move forward once the first half of requests are successfully processed (and gracefully handle cancelling the other outstanding requests).

Blog workflow 

Now that we’ve built the workflow, we can incorporate it into an ASP.NET MVC application. Here we’ll use the WorkflowInvoker to execute the workflow from a controller.

public ActionResult GetBlogs()

{

    var results = WorkflowInvoker.Invoke(

        new GetSomeBlogs(),

        new Dictionary<string, object>

        {

            {

                "Urls", new List<string>

                {

                    "https://blogs.msdn.com/somasegar/atom.xml",

                    "https://blogs.msdn.com/brada/atom.xml",

                    "https://blogs.msdn.com/endpoint/atom.xml",

                    "https://blogs.msdn.com/mwinkle/atom.xml"

                 }

             }

        });

    var feeds = results["Feeds"] as List<SyndicationFeed>;

    ViewData["Message"] = "You received " +

                          feeds.Count.ToString() +

                          " feeds with a total of " +

                          feeds.Sum(feed => feed.Items.Count()) +

  " posts.";

    return View("Index");

}

Workflows and WCF Services

As we talked with customers, we found that many were interested in using WF with services to compose and coordinate messaging in their application. In .NET 4, we set out to make it easy to use workflows with services to compose and coordinate messaging by integrating WF with Windows Communication Foundation (WCF).

WF comes with messaging activities that allow you to expose service operations as part of the workflow, and can be used to enable fine-grained control over the way inbound messages are correlated to workflow instances. Additionally, you can use WorkflowServiceHost to host your services within IIS7 and Windows Server AppFabric, eliminating the need to write custom hosting logic. If you are writing a workflow that uses web services to communicate, WorkflowServiceHost frees you from writing an application around your workflow service. WorkflowServiceHost and the workflow runtime will ensure workflows are automatically persisted and restarted when needed, with the correct state information loaded. The runtime addresses the complexities of managing the resources and state of the workflow, including managing the flow of transactions into and out of the workflow.   

Find Out More

Visit the WF MSDN Dev Center and the Endpoint team blog to learn more, or connect with the Workflow team on the MSDN forums.  To get started with Workflow Foundation, check out following whitepapers: The Workflow Way: Understanding Windows Workflow Foundation and A Developer’s Introduction to WF4.

Namaste!