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.
To keep things easy, we will create a very simple greeting application. The user will type his name into a TextBox, click a button, and a greeting with his name will appear. Sounds simple? It is!
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).
For now, just delete the Activity1.xaml file.
Create a new Web Form and name it Default.aspx:
We need four controls on our site:
The code is unspectacular; you only need a click event on the Button control:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Workflow.Web.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label Text="Your name: " runat="server" /> <asp:TextBox ID="TextBoxName" runat="server" /> <asp:Button ID="ButtonCreateGreeting" Text="Create greeting" runat="server" onclick="ButtonCreateGreeting_Click" /> <br /> <asp:Label ID="LabelGreeting" Text="" runat="server" /> </div> </form> </body> </html>
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:
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:
protected void ButtonCreateGreeting_Click(object sender, EventArgs e) { string username = TextBoxName.Text; Greeting greeting = new Greeting { ArgUserName = username }; IDictionary<string, object> results = WorkflowInvoker.Invoke(greeting); LabelGreeting.Text = results["Result"].ToString(); }
Does it look like 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 a 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:
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.
The sample application can be download here.
This article is also available in the following languages:
Deutsch (de-DE)
Horizon_Net edited Original. Comment: Updated code formatting.
Good Article
Carsten Siemens edited Revision 19. Comment: fixed typo
Nice to read and understand
very illustrative
great !!