Click here to Skip to main content
15,886,664 members
Articles / Operating Systems / Windows

Getting Started: Windows Workflow Foundation and ASP.NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Jan 2012CPOL3 min read 6.4K   1  
Getting Started: Windows Workflow Foundation and ASP.NETWindows Workflow Foundation is a great technology for creating workflows. It can be used in

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Getting Started: Windows Workflow Foundation and ASP.NET

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.

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:

<span style="background: yellow;"><%</span><span style="color: blue;">@</span> <span style="color: maroon;">Page</span> <span style="color: red;">Language</span><span style="color: blue;">=</span><span style="color: blue;">"C#"</span> <span style="color: red;">AutoEventWireup</span><span style="color: blue;">=</span><span style="color: blue;">"true"</span> <span style="color: red;">CodeBehind</span><span style="color: blue;">=</span><span style="color: blue;">"Default.aspx.cs"</span> <span style="color: red;">Inherits</span><span style="color: blue;">=</span><span style="color: blue;">"Workflow.Web.Default"</span> <span style="background: yellow;">%></span>
 
<span style="color: blue;"><!</span><span style="color: maroon;">DOCTYPE</span> <span style="color: red;">html</span> <span style="color: red;">PUBLIC</span> <span style="color: blue;">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color: blue;">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: blue;">></span>
 
<span style="color: blue;"><</span><span style="color: maroon;">html</span> <span style="color: red;">xmlns</span><span style="color: blue;">=</span><span style="color: blue;">"http://www.w3.org/1999/xhtml"</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">head</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span><span style="color: blue;">></span>
    <span style="color: blue;"><</span><span style="color: maroon;">title</span><span style="color: blue;">></</span><span style="color: maroon;">title</span><span style="color: blue;">></span>
<span style="color: blue;"></</span><span style="color: maroon;">head</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">body</span><span style="color: blue;">></span>
    <span style="color: blue;"><</span><span style="color: maroon;">form</span> <span style="color: red;">id</span><span style="color: blue;">=</span><span style="color: blue;">"form1"</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span><span style="color: blue;">></span>
    <span style="color: blue;"><</span><span style="color: maroon;">div</span><span style="color: blue;">></span>
        <span style="color: blue;"><</span><span style="color: maroon;">asp</span><span style="color: blue;">:</span><span style="color: maroon;">Label</span> <span style="color: red;">Text</span><span style="color: blue;">=</span><span style="color: blue;">"Your name: "</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span> <span style="color: blue;">/></span>
        <span style="color: blue;"><</span><span style="color: maroon;">asp</span><span style="color: blue;">:</span><span style="color: maroon;">TextBox</span> <span style="color: red;">ID</span><span style="color: blue;">=</span><span style="color: blue;">"TextBoxName"</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span> <span style="color: blue;">/></span>
        <span style="color: blue;"><</span><span style="color: maroon;">asp</span><span style="color: blue;">:</span><span style="color: maroon;">Button</span> <span style="color: red;">ID</span><span style="color: blue;">=</span><span style="color: blue;">"ButtonCreateGreeting"</span> <span style="color: red;">Text</span><span style="color: blue;">=</span><span style="color: blue;">"Create greeting"</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span> 
            <span style="color: red;">onclick</span><span style="color: blue;">=</span><span style="color: blue;">"ButtonCreateGreeting_Click"</span> <span style="color: blue;">/></span>
        <span style="color: blue;"><</span><span style="color: maroon;">br</span> <span style="color: blue;">/></span>
        <span style="color: blue;"><</span><span style="color: maroon;">asp</span><span style="color: blue;">:</span><span style="color: maroon;">Label</span> <span style="color: red;">ID</span><span style="color: blue;">=</span><span style="color: blue;">"LabelGreeting"</span> <span style="color: red;">Text</span><span style="color: blue;">=</span><span style="color: blue;">""</span> <span style="color: red;">runat</span><span style="color: blue;">=</span><span style="color: blue;">"server"</span> <span style="color: blue;">/></span>
    <span style="color: blue;"></</span><span style="color: maroon;">div</span><span style="color: blue;">></span>
    <span style="color: blue;"></</span><span style="color: maroon;">form</span><span style="color: blue;">></span>
<span style="color: blue;"></</span><span style="color: maroon;">body</span><span style="color: blue;">></span>
<span style="color: blue;"></</span><span style="color: maroon;">html</span><span style="color: blue;">></span>

Create the Workflow

Open the 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.

Adding a Sequence Activity

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:

<span style="color: blue;">protected</span> <span style="color: blue;">void</span> ButtonCreateGreeting_Click(<span style="color: blue;">object</span> sender, <span style="color: rgb(43, 145, 175);">EventArgs</span> e)
{
<span style="color: blue;"> string</span> username = TextBoxName.Text;
 
<span style="color: rgb(43, 145, 175);"> Greeting</span> greeting = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Greeting</span> { ArgUserName = username };
 
    IDictionary<<span style="color: blue;">string</span>, <span style="color: blue;">object</span>> results = <span style="color: rgb(43, 145, 175);">WorkflowInvoker</span>.Invoke(greeting);
    LabelGreeting.Text = results[<span style="color: rgb(163, 21, 21);">"Result"</span>].ToString();
} 

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.

Download

The sample solution can be downloaded from the MSDN Samples.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --