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:
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).
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:
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:
Add a Sequence Activity to it. The Sequence Activity ensures that the child activities runs according to a single defined ordering.
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.
Please note, every expression in the Workflow Designer must be a Visual Basic expression.
The result should look as follows:
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:
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.