Windows Workflow Foundation with ASP.NET

Introduction

Windows Workflow Foundation is a great technology for creating workflows. It can be used in combination with different technologies, for example SharePoint, WCF, etc. In this article we will combine the Windows Workflow Foundation with ASP.NET.

You will learn how to build a simple workflow with an input and an output and how to integrate this workflow in a simple ASP.NET application.

The download contains the code files of the sample ASP.NET website and Windows Workflow foundation workflow which are used in the greeting example of this article.

The Scenario

To hold things easy, we will create a very simple greeting application. The user will type in his name into a TextBox, clicks a button and a greeting with his name will appear. Sounds simple? It is!

File – New – Project

Start by creating an empty Visual Studio Solution:

Create a blank solution

Name it whatever you want. We will now add two projects to this solution – An ASP.NET Empty Web Application (Workflow.Web) and an Activitiy Library(WorkflowLibrary).

Create the ASP.NET Web Application

Create the Activity Library

Set it up

On the side of our Workflow

For now, just delete the Activity1.xaml file.

On the side of our Website

Create a new Web Form and name it Default.aspx:

Add the Web Form

We need four controls on our site:

  • A Label, which only shows “Your name: “; no more functionality
  • A TextBox, where the user can type in his name
  • A Button, which will be trigger the workflow
  • A Label, which displays the result of the workflow, our greeting

The code is unspectacular; you only need a click event on the Button control:

   1:  <%@ Page Language="C# AutoEventWirkup="true" CodeBhind="Default.aspx.cs" Inherits="Workflow.Web.Default" %> 
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml"> 
   6:  <head runat="server"> 
   7:      <title></title> 
   8:  </head> 
   9:  <body> 
  10:      <form id="form1" runat="server"> 
  11:      <div> 
  12:          <asp:Label Text="Your name: " runat="server" /> 
  13:          <asp:TextBox ID="TextBoxName" runat="server" /> 
  14:          <asp:Button ID="ButtonCreateGreeting" Text="Create greeting" runat="server" onclick="ButtonCreateGreeting_Click" /> 
  15:          <br /> 
  16:          <asp:Label ID="LabelGreeting" Text="" runat="server" /> 
  17:      </div> 
  18:      </form> 
  19:  </body> 
  20:  </html>

Create the Workflow

At the beginning, create a new activity and name it Greeting.xaml. First we need an In argument and an Out argument. The In argument will take the name of our user. The Out argument will hold the greeting and will be assigned to the greeting label on our website.

Open the Arguments tab at the bottom of the designer. The first argument has the name ArgUserName, the direction In and the Argument type String. The second argument takes the name Result, the direction Out and the Argument type String, too.

In both cases you can leave the cell for the Default value empty. The Result will look like this:

Creating the In and Out arguments

Add a Sequence Activity to it. The Sequence Activity ensures that the child activities runs according to a single defined ordering.

AddSequence

Inside the Sequence Activity add an Assign Activity. This activity will assign the greeting to our earlier created Result argument.

The To property will be our Result argument. The Value property can be created with the Expression Editor. The next picture shows the expression.

Assign the greeting to the Out argument

Please note, every expression in the Workflow Designer must be a Visual Basic expression.

The result should look as follows:

The finished workflow

Combine it

To complete our application, add a reference to the WorkflowLibrary in our web application. We also need the WorkflowInvoker class, so add a reference to System.Activities to our web application.

In the click method of our button add the following code:

   1:  using System; 
   2:  using System.Activities; 
   3:  using System.Collection.Generic; 
   4:  using WorkflowLibrary; 
   5:   
   6:  namespace Workflow.Web 
   7:  { 
   8:      public partial class Default : System.Web.UI.Page 
   9:      { 
  10:          protected void Page_Load(object sender, EventArgs e) 
  11:          { 
  12:   
  13:          } 
  14:   
  15:          protected void ButtonCreateGreeting_Click(object sender, EventArgs e) 
  16:          { 
  17:              string username = TextBoxName.Text; 
  18:   
  19:              Greeting greeting = new Greeting { ArgUserName = username }; 
  20:   
  21:              IDictionary<string, object> results = WorkflowInvoker.Invoke(greeting); 
  22:              LabelGreeting.Text = results["Result"].ToString() 
  23:          } 
  24:      } 
  25:  }

Does it looks light magic? Not really.

First we retrieve the entered name from the TextBox. Next we need an instance of our workflow and pass in the username as the In argument. To invoke our Workflow we call the Invoke-method with our workflow instance. It retrieves the Out arguments as an Dictionary, where the key will be a string and the value an object. As the last thing we just have to get our Result argument out of the Dictionary and assign it to our greeting Label.

That’s it! Run the project! The result should look like this:

The result

Lessons Learned

We have seen how to create a simple workflow with In and Out arguments and how values could be assigned. We have also seen how to invoke a workflow, how to pass in arguments and how to retrieve arguments from the workflow. At the UI side was nothing special and you can work similar in other technologies, such as Silverlight.

– Jan (@Horizon_Net)

Advertisements

2 thoughts on “Windows Workflow Foundation with ASP.NET

  1. sunil

    hi I would like to implement similar logic in Azure platform. What is your comments. should i look at app logics, bizz talk or simply use WWf .net 4 framework

    Reply
    1. Jan Hentschel Post author

      I haven’t done much work in this area lately, but Paul Baars has written a short article where he mentions that you can use all three of them in conjunction. In my opinion Logic Apps are a good solution in integration scenarios (as shown by Scott Hanselmann and Josh Twist in this video). In case you have a more classical workflow WF is still a good way to do it, but you have to decide how to host it. If you decide to host a WF workflow in a Web API you can orchestrate it with other workflows (maybe also built with WF) in a Logic Apps, which works as a coordinator in this case.

      Reply

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