Workflow Foundation 4.0

Table of Contents

Introduction

The goal of this article is to help developers get familiar with Worflow Foundation 4.0.

Workflow Foundation 4.0

It provides a programming model, in-process workflow engine and rehostable designer to implement long-running processes as workflows within .NET applications.MSDN

Building a WCF service using WF4.0

This article will provide a step-by-step guide to build and test a simple calculator web service in WF4.0. I will be using mostly built-in WF activities to build workflow. In addition, I will build a simple custom code activity to go over the custom activities.

So the task of the service will be to take three parameters, two for operand and one for operation name and return the result back to the client after applying the operation. The diagram below explains the complete flow that we are going to implement during this exercise and our workflow will look like this flow chart at the end.

Building workflow application will be similar to building this flow chart. Each box in the above diagram will be called activity and all the activities will be contained in a larger activity called Main Activity. Most of the activities needed to build this application will be provided by the Visual Studio 2010 and we will add a new custom activity to log the data into a file to track the application.

Workflow Foundation 4.0

Lets get started

Start New Project

1. Launch Visual studio

2. Select Project->New

3. Select "WorkFlow" under "Installed Templates->Visual C#"

4. Select "WCF Workflow Service Application"

5. Change the name of the project to "CalculatorWFService"

6. Click OK to close this window

Image1.png

This will create a project using a Workflow Serivce Template with three built-in activities Sequense, ReceiveRequest and SendResponse activities (see the designer window in the figure below).

Image2.png

Sequence Activity:

The SequenceActivity is a CompositeActivity, meaning the SequenceActivity can contain other activities. The SequenceActivity class coordinates the running of a set of child activities in an ordered manner, one at a time. The SequenceActivity is completed when the final child activity is finished.

Receive Activity:

Receive activity will wait until a client connects to the service and then run its contained child activities. MSDN

Send Activity:

Client activity that models the synchronous invocation of a service operation.

Rename service operation name and contract name:

1. Click on the sequence service to Main Sequence (to match workflow in fig 1). This renaming will not have any effect on the application but it will improve the readability of the workflow.
2. Rename the Service1.xamlx file to CalculatorService.xamlx by right clicking on the file.
3. By default the workflow service template configures the service with "GetData" web operation name (think of it as a web method). To rename this method click on the "ReceiveRequest" activity and change the "Operation Name" property to "Calculate". Also, change the service contract property to ICalculate.

Add top level variables

Select the activity called "Main Sequence" and click on the "variables" button at the bottom of the designer window.

[Image3.png]

This action will open up a variables window (highlighted below)

[Image4.png]

Remove the "data" variable by selecting the complete row. As soon as the variable "data" is deleted the designer will show error icon on the activities which no longer have the "data" variable that they were using. This is a very helpful feature for debugging and tracking design time issues.

[Image5.png]

For now let's just ignore these error icons and keep moving on. They will disappear after we are done with the activity configurations.
Add variables to hold the three input parameters by clicking on the "Create Variable" text in the variable window. Create two parameters of type int32 (use the drop down control in the "variable type" column to select the variable type) and name them "Operand1", "Operand2". Create a third variable of type "string" and name it "OperationName". Also let's add a fourth variable called "Result" of type string to hold the result (see the screen shot below) and send it back to the caller application.

[Image6.png]

These variables are just like adding class level variables which will be accessible to all the class methods. In this case, they will be available to all the activities within the "Main Sequence" activity.

Workflow Foundation 4.0

Receive Activity Configuration

Now we have to configure Receive activity to accept following three parameters. To do this you will have to select the receive activity and change its "Content" property by clicking [dotdot.png] button. This will open up the "Content Definition" window.Click on the parameter radio button to define parameters. Message data option can be used to receive custom class objects.

[Image8.png]

Define three parameters (as shown in the figure above) by clicking on the "Add new parameters" text. "Parameter1" and "parameter2" will hold two numbers and "operationName" will hold the name of the operation to be performed on these numbers.

Parameter to workflow variable mapping

After adding parameters we have to assign workflow level variables to the parameters so that all the input data will be available for all workflow activities. To do this just select the "Enter a VB expression" text in the cell next to the parameter name and type in the variable that you want to assign to the parameter. In this exercise I will assign parameter1 to operand1, parameter2 to operand2 and operationName to OperationName workflow variable.

Image9.png]

Click OK to close the window.

Send Activity Configuration

To configure the send activity select the "SendResponse" activity in the designer. Change its "Content" property to configure what to send back to the caller. Select the [dotdot.png] button next to "Contents in the property box. This will open up a new "Content Definition" window same as what we have seen before during the receive activity configuration. We do not have to configure parameters here Message configuration will just do fine. Just remove "data.ToString()" with "Result" variable that we have declared in the beginning. On more great features of the designer is that it provides intelli-sense help throughout the process and that makes it easier to select and find variables available (see screen shot below)

[Image11.png]

Click OK to close the window and get back to the designer.

Now we are done with the web service input and output configurations. Next step is to perform some operation based on the "operationName" parameter. For that we are going to use a flow chart activity. Before we start build a flow chart let's build a custom code activity to log some trace information in a file and also add some text to the final result. We will use this activity in the flow chart.

Steps to build a custom code activity

1. Right click on the CalculateWFService project and select add new item.
2. The in the installed template section select "workflow"
3. Select the "Code Activity"
4. Click "Add" to add this activity to the project (we will leave the default name "codeactivity1.cs" as is)

[Image12.png]

5. Every code activity can have input and output arguments just like any C# methods has input and output parameters. I find it easier to think of it as a C# class method and the way it deals with the input and output parameters. The only exception is that in code activity they have to be declared as InArguments and OutArguments and have to be accessed via current context. In this code activity we are going to use the default text InArgument and will add an OutArgument "result". Just type the following in the top section of the class.

public OutArgument<string> result { get; set; }

6. Add following simple execution code in the Execute method of the code activity class.
File.AppendAllText(@"C:\WorkflowLog.txt", "Result is:" + text);
context.SetValue(result, "Result is:" + text);

All this code is doing is writing the input of this code activity in a file and returning the input of the activity after appending some text in it. Also, you will have to add the following in the file header:

using System.IO;
In code activities all the activity execution logic should be written in the Execute method. Please check the following link for more information on code activities and execute method:
"http://msdn.microsoft.com/en-us/library/ee264176.aspx".

7. Build the project at this point to check for any errors and to add this new code activity in the "Toolbox"

[Image13.png]

8. Select the "CalculatorService.xamlx" to go back to the designer

Flowchart

There are other options to get the result and this simple task can be accomplished by using one custom code activity or via simple expression in the send activity's configuration. But my goal is to give overview of all as much workflow features as possible in this exercise and this is why I am going to use a flow chart.

What is flowchart activity?

A Flowchart activity is an activity that contains a collection of activities to be executed. Flowcharts also contain flow control elements such as FlowDecision and FlowSwitch that direct execution between contained activities based on the values of variables.

Types of Flow Nodes

Different types of elements are used depending on the type of flow control required when the element executes. Types of flowchart elements include:

FlowStep

Models one step of execution in the flowchart.

FlowDecision

Branches execution based on a Boolean condition, similar to If.

FlowSwitch

Branches execution based on an exclusive switch, similar to C# Switch. Each link has an Action property that defines a ActivityAction that can be used to execute child activities, and one or more Next properties that define which element or elements to execute when the current element finishes execution. You can fine out more about flowcharts here.

Ok enough about flowcharts let's get started on building one to calculate the result.

Workflow Foundation 4.0

Switch Activity Configuration

1. Select "Switch" in the designer window
2. Click on the button next to the "Expression" in the property window
3. Type in "OperationName" in the expression editor (Intelli-sense will help you select the variable) and click OK to close the window
4. Now the switch is configured to evaluate the "OperationName" variable and to decide the path that it will have to take based on its value. So let's define different paths for different values. In this exercise I am going to only perform three calculations, addition, subtraction and multiplication and if operationName is not "add", "subtract" or "multiply" then I will return "not implemented" message (take a look at the flow chart in the beginning of the article). Select the "CodeActivity1" in the toolbox and drop it four times in the flowchart designer window.
5. Draw lines from "Start" node to "switch" node and then draw four lines from "Switch" node to each of the "CodeActivity1". To draw a line just hover the mouse pointer over an activity and drag a line to the next activity.
6. Select each line except the "default" line and change "Case" in the property window to "add", "subtract" and "multiply" as shown in the figure below:

[Image17.png]

If you remember "Text" is the input argument for the code activity and "result" is the output argument. So in the following few steps we are going to set these input and output arguments for all the code activities that we dropped in the designer.

7. Select the "CodeActivity1" which is linked to the "add" case and type the "(Operand1 + Operand2).toString()" expression in the "Text" property. Type "Result" in the "result" property expression
8. Select the "CodeActivity1" which is linked to the "subtract" case and type the "(Operand1 - Operand2).toString()" expression in the "Text" property. Type "Result" in the "result" property expression
9. Select the "CodeActivity1" which is linked to the "multiply" case and type the "(Operand1 * Operand2).toString()" expression in the "Text" property. Type "Result" in the "result" property expression
10. Select the "CodeActivity1" which is linked to the "default" case and type the "Not implemented" expression in the "Text" property. Type "Result" in the "result" property expression

[Image18.png]

The flowchart activity will check the input variable "OperationName" and will take the appropriate execution path. At the end on each path we are setting the workflow level variable called "Result" to some value via "codeActivity1". After the flow chart the workflow will execute the send activity which will result the "Result" value back to the caller.

So this is it folks, our workflow service is ready.

WCF Web Service Test (using WCFTestClient)

Now let's test this using the WCFTESTClient.exe. This exe can be found under "C:\Program Files \Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe" path. This tool can be used to test any WCF web service. Just follow the steps below to test our calculator service:

1.
Right click on the project and select properties
2. Click on "WEB" tab on the left of the properties page
3. Click on "Start external program" radio button
4. Type WCFTestClient.exe path in the textbox (C:\Program Files \Microsoft Visual Studio 10.0\Common7\IDE\WcfTestClient.exe)
5. Save the application.
6. Let's get the web service reference path by right clicking on the ".xamlx" file in the visual studio's solution explorer window and selecting "Browse" option for the menu. We will need this for the test utility.

[Image19.png]

7. Copy the web service link from the browser

[Image20.png]

8. Press F5 to run the application
9. This will launch the WCFTestClient application see the screen shot below:

[Image21.png]

10. Right click on the "My Service Project" to add the web service reference and paste the service path that we copied in step 7. This will add the web service reference in the WCF test client application.
11. Click on Calculate in the left window and then expand it in the right side window to see all the input parameters. Set the input parameter values and click "Invoke" to invoke the web service method. The bottom "Response" window will display the result. Take a look at the screen shot below:

[Image22.png]

Hope this exercise gives you some insight into Windows Workflow Foundation. In my next article I will try to deploy this in IIS and will configure it for monitoring using AppFabric. Also I will try to go over some of basic and most important workflow features like persistence, workflow execution and monitoring using AppFabric.


Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds