Windows Workflow Foundation in .NET4

Windows Workflow Foundation in .NET4

  • Comments 24

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>

                {

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

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

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

                    "http://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!

Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post
  • You neglect to mention that none of WF4 is available for use in SharePoint 2010 workflows, which is still stuck with WF3.

  • Hi Soma!

    WF4 is great stuff. But why force us (C# developers) to use VB expressions in the designer? It seems very awkward when working in a purely C# environment except for the expressions in the WF designer. Any way of configuring WF4 to use C# expressions instead?

  • I worked with Workflow (Beta .Net framework 3.0) approx 2.5 yrs back. But it was a bit confusing & finally we re-worked the application. Let's hope WF in .Net 4 is easier to implement.

  • Forcing developers to use VB expression in the designer sucks.  I hate it.

  • Hi,

    i working on an HRMS application. and i want to ask

    can i use the workflow services with ORACLE 11g.

    and if not then how can i use that for my workflow activity persistence.

    Thanks.

  • Just want to echo what others are saying re the VB expression syntax.  This *SUCKS* big time.

  • VB was never a proper languauge... and continues to be a second choice even though Microsoft evangelists tout it and push it

  • Ever tried to teach a business process modeller these fine points of c#'s obligatory syntactic sugar?

    A = DateTime.Now  <- field/property access

    B = A.ToString() <- parameterless function, that is a property

    C = o.SomeFunction(0) <- an integer parameter

    D = o.SomeArray[0] <- an array index, i.e. an integer parameter

    E = "c:/a/b/c/d.txt"

    F = @"c:\a\b\c\d.txt"

  • Seems WF 4 is finally done right

  • Usama Khalil,

    Out of the box, we have an implementation of our persistence features that target SQL Server. If you wanted to target Oracle for workflow services, you would need to provide an implementation of the instance store that would handle writing to the database.   There's a very basic file based InstanceStore implementation in the PurchaseProcess sample in the WF SDK.

    --matt

  • I cannot for the life of me find any documentation on how to implement a long running workflow...So for example My flow would email 4 people, and wait for all 4 to respond (persist to a DB?) before continuing on to (for example) email another 4 people...so like an approval process

    The documentation for "Hello World" instant flow is allover the place

    Help!  I want to sue this so bad :)

  • Regarding the use of VB expressions throughout workflow, we're actively investigating and working with the C# team to incorporate this into a future release.  A little bit of background can be found in the two "Future of..." talks at PDC08 that Anders and Paul did on C# and VB.  Currently, VB was able to let us use an in-memory compiler, which is the technology we rely on in order to enable expressions.  

    We had a fair amount of feedback from WF3 customers that they wanted the ability to have a more expressive way to do data binding between activities, which led us to making the decision to add expressions in the WF4 release.  This enables much greater flexibility when interacting with data in the workflow.  We currently have the VB compiler available which is why WF4 uses VB expressions everywhere.  

    If you have more questions, or feedback on this, please feel free to email me.

    --matt winkler

    --pm, wf team

    --mwinkle at microsoft dot com

  • @stevescottwork,

    We have a couple of samples which show a similar process to what you are asking for:

    HiringProcessRequest sample: This shows using WorkflowServiceHost and the messaging activities http://msdn.microsoft.com/en-us/library/ee622985(VS.100).aspx

    Purchase Process sample: This uses WorkflowApplication as the host http://msdn.microsoft.com/en-us/library/dd807514(VS.100).aspx

    There are two good intro docs as well

    Getting Started : http://msdn.microsoft.com/en-us/library/dd489454(VS.100).aspx

    Windows Workflow Foundation Programming: http://msdn.microsoft.com/en-us/library/dd489462(VS.100).aspx

    Let me know if that helps!

    --matt winkler, pm wf team

  • Where are the VS 2010 RTM bits? They should be up on MSDN by now.

  • Sambo, VS2010 will be available at launch on April 12th.

    Polita Paulus

    Microsoft

Page 1 of 2 (24 items) 12