Search This Blog
2009-06-16
Basics Of Windows Workflow Foundation
A set of activities that coordinate people
and / or software.
What are Activities?
An activity is a step or unit that represents runtime logic in a workflow.is the basic building block.An activity can expose properties and events that can be programmed againist within a workflow. An activity can also contain methods that are invoked by the workflow designer or workflowRuntime.
Properties and events that are exposed to the outside world and can be programmed from the workflow.It Supports pre/post interception hooks into execution in user code.
Methods which implement the inner behavior and are called by the workflow runtime.
What are the different categories of Activity?
Activites fall under two broad categories:
- Basic or sometimes called Primitive activities, which are steps in a workflow that execute runtime logic.Basic activities are steps that “do work”
- composite – which manage a set of child activities.In fact, a workflow itself is an activity.One variant of composite activites is a root activity.Workflows are just a special kind of composite activity
What are the different Types Of Workflow?
There are two types of workflow:
1.Sequential
2.State Machine
A state-based workflow waits on external entities to perform some action before moving on to the next step. On the other hand, a sequential workflow is just a continuous flow of operations which might include branching, but steps in the sequential workflow don't wait for an external entity to perform the operation, and then continue.
Which workflow to choose ?
Sequential workflow follows the traditional style of thinking - i.e. as long as the process is simple and stays in the boundary then a sequential workflow will work.
On the other hand, state machine workflows deal with different states of the workflow. A process which involves several different iterations before getting into the final shape is a candidate for being a state machine workflow.
Why Workflows and WF?
One of the biggest reasons for creating a workflow is that you are actually creating a model. Most of the business processes have some sort of model associated with them, whether it is use-cases, UML diagrams or a simple flow chart. In a process you always have some flow and thus, there will always be some steps involved in its execution.
With WF, model and workflow are one and the same. You use pieces of a business process to make a workflow and you piece together the workflow, which makes a model. Before WF you would make couple of UML diagrams, drawing flow-charts, writing pseudo code, explaining in bulleted text how an operation should work and then finally you will get to writing the code, whereas with WF when you are modeling the business process you are also building the application!
What is the Host Process for WF?
A WF has no executable environment and requires a host process within which it can execute. They are not applications themselves, rather, they require an application where they can reside. This host process can be a Windows Application or an ASP.NET Web application.
What is Workflow RunTime Engine?
The WF runtime engine executes workflow made up of activities created with VS2005 Workflow Designer. The runtime engine includes three basic services:
-1.Scheduling (schedules the execution of activities within a workflow)
-2.State Management (allows the state of the workflow to be persisted instead of storing it in some other mechanism like database)
-3.Rules (executes policy activities i.e., add rules on the workflow against certain activities in order to control the flow)
2009-04-26
Tutorial 2:Receiving Data into the Workflow using Parameters
In this exercise, you will modify the workflow we created in Tutorial 1 to receive data into our workflow when it is started. You will also modify the code in our Code activity’s handler to display the input values. Finally, you will change our startup project to use a Windows Forms (WinForms) host that will allow us to enter the input values and create an instance of our workflow.
Note: There are two general approaches for receiving data into a workflow when it is started. They are Parameters and Events. With parameters, a list of parameter names and types are defined with the workflow. These parameter values are passed in by a host when it starts a new instance of the workflow type. With events, workflow authors add an activity that receives an event and data associated with the event. Events are generally specific to the host and custom activities that have been designed to handle the event.
Task 1 – Define parameters for the workflow
1. In Visual Studio 2005 with the HelloWorldWorkflow project open, double click on the Workflow1.xoml file in Solution Explorer to open the Visual Studio workflow designer. 2. Right-click the workflow background in the Visual Studio workflow designer and click View Code, or select the View Code menu item. 3. Inside the Workflow1 class, type “prop” and select prop from the intelli-sense menu, to insert the snippet hit tab.
5. Use the same technique to include another string property called LastName with the private member lastName.
Task 2 – Modify the Code Activity
1. In this exercise we will use a Windows Forms application as the workflow host. Since we want to display a message box from the workflow application, we need to add a reference to the assembly for Windows Forms. Select the Project Add Reference menu item.
2. On the .NET tab in the Add Reference dialog, select System.Windows.Forms from the list of assemblies and click the OK button.
3.Right click on the Workflow1.xoml.cs file in Solution Explorer and select the View Code menu item from the context menu. Navigate to the method codeActivity1_CodeHandler in the 1. Workflow1.xoml.cs source file. This method is the code handler for the code activity in the workflow.
4. Modify the code in the codeActivity1_CodeHandler method to get the parameter values and display a Message Box with the FirstName and LastName values:
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show(
"Hello world: " + firstName + " " + lastName);
}
Task 3 – Execute the modified workflow
Now we’re ready to test the modified workflow. Instead of using a console host application as we did in Exercise 1, in this exercise we’re going to add a new Windows Forms application and some pre-written code as a host for our workflow.
1. In Visual Studio 2005, with the HelloWorldWorkflow solution open, add a new project, WinFormTestHost to the solution by selecting the File Add New Project menu item as shown below:
3.Design the Windows forms as follows
Step 5. Write the following code in the windows application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace WinFormTestHost
{
public partial class Form1 : Form
{
private WorkflowRuntime wr;
private string workflowAssembly = "";
private string workflowTypeName = "";
public Form1()
{
InitializeComponent();
workflowAssembly = "HelloWorldWorkflow";
workflowTypeName = "HelloWorldWorkflow.Workflow1";
}
private void btnStartWorkflow_Click(object sender, EventArgs e)
{
if (wr == null)
{
wr = new WorkflowRuntime();
wr.StartRuntime();
}
Dictionary
parameters.Add("LastName", txtLastName.Text);
WorkflowInstance instance = wr.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1), parameters);
instance.Start();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (wr != null)
{ if (wr.IsStarted)
{ wr.StopRuntime();
}
}
}
}
}
4. Using the Solution Explorer tool window, right click on the WinFormTestHost project and select Set as Startup Project from the context menu.
5. Now we need to make sure that the WinFormTestHost application can use the new workflow. Add a reference to the HelloWorldWorkflow project by right clicking on the References folder for the WinFormTestHost in Solution Explorer and selecting Add Reference from the context menu.
6. In the Add Reference dialog window, select the Projects tab.
7. Select the HelloWorldWorkflow project from the list and press the OK button to close the Add Reference dialog window and add the new reference.
8. We also need to ensure that the WinFormTestHost has references to the Windows Workflow Foundation system assemblies. Add these references by right clicking on the References folder for the WinFormTestHost in Solution Explorer and selecting Add Reference from the context menu
9. In the Add Reference dialog window, select the .NET tab, scroll down and select the entries shown below and click OK.
10. Right click on the Workflow1.xoml.cs file in Solution Explorer and select the View Code menu item from the context menu to open the code-beside class for the workflow.
11. Set a breakpoint in the codeActivity1_CodeHandler method.
12. Compile and run the solution under the Visual Studio debugger by pressing F5 or selecting the Debug Start Debugging menu command.
13. The WinFormTestHost will start and display a window containing fields for entering a First and Last Name.
14. Enter values in the First name and Last name fields
15. Click the Start Workflow button to instantiate a new instance of the HelloWorldWorkflow.Workflow1 and pass in the First name and Last name values as parameters.(Images are same as Task3-Step 3)
16. Visual Studio 2005 will then break into debug mode for the codeActivity1_CodeHandler method. Continue execution of the workflow by selecting the Debug Continue menu item.
17. When the code activity finishes executing you will see a message box with the first name and last name were passed as parameters to the workflow.
Reference:http://msdn.microsoft.com/workflow
Tutorial 1:Create a Simple Workflow
1. Open Visual Studio 2005 by going to the Start Menu Programs Microsoft Visual Studio 2005 Microsoft Visual Studio 2005
2. In Visual Studio 2005, select the File New Project menu command.
3. Visual Studio 2005 will display the New Project dialog window.
4. In the New Project dialog window, expand Visual C# Workflow in the Project Types tree on the left side.
5. Select the template named “Sequential Workflow Console Application” and enter the following values:
Name: HelloWorldWorkflow
Location: C:\Windows Workflow Foundation\Labs\Lab01
6. You should now have a new solution and workflow project.
7. The project template has a Workflow1.cs which we will remove as we will be using a simpler kind of workflow for this lab. Click on Workflow1.cs and press the DEL key. Click the okay button to confirm.
8. Right click on the HelloWorldWorkflow project in Solution Explorer and select Add New Item from the context menu.
9. In the Add New Item dialog window, select the item template named Sequential Workflow (with code separation) and click the Add button to add the new workflow to the HelloWorldWorkflow project.
Note: It may be easier to read the template names if you change the view of the Add New Item dialog to large Icons by clicking on the toolbar icon in the upper right corner.
10. The new Sequential Workflow project will now contain two files: Workflow1.xoml and Workflow1.xoml.cs. Workflow1.xoml contains the XML markup that represents the workflow type. Workflow1.xoml.cs contains the code-beside class for the workflow.
Task 2 – Add a code activity
1. Double click on the Workflow1.xoml file in Solution Explorer to open the Visual Studio workflow designer.
2. In the workflow designer, select the View Toolbox menu command to display the toolbox with available activities.
3. You may need to expand the Windows Workflow category in the toolbox to view the Windows Workflow Foundation activities.
4. Select the Code activity from the toolbox.
5. Drag and drop the Code activity to the design surface for the Sequential Workflow.
6. Notice the red exclamation mark on the new Code activity. Click the exclamation mark to view the smart tag and the reason for why it’s being displayed.
7. Selecting the smart tag will cause the Properties tool window to be displayed with the ExecuteCode property highlighted.
8. Enter the value codeActivity1_CodeHandler for the ExecuteCode property and press Enter.
9. This will cause a new method or handler to be added to the code-beside class for the workflow. Visual Studio will automatically open the Workflow1.xoml.cs code file and display the new codeActivity1_CodeHandler method. Alternatively, you can switch from the design view to the code-beside for the workflow by double-clicking on the activity or by selecting the View Code menu item.
10. The code-beside class is a partial class that inherits from the SequentialWorkflowActivity base class. This code-beside class will have the same name as the workflow. You should have the following code in your code-beside class:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace HelloWorldWorkflow
{
public partial class Workflow1 : SequentialWorkflowActivity
{
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
}
}
}
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
Console.WriteLine("Hello, World!");
}
For this exercise we are going to use the simple host in program.cs that comes with the project template that was selected. Alternatively, you could build your own workflow host application by using the workflow runtime APIs defined in the System.Workflow.Runtime namespace.
1. Open the Visual Studio workflow designer by double clicking on workflow1.xoml in the solution explorer.
2. Click on the codeActivity1 activity to select it and set a breakpoint on it.
3. To set the breakpoint right-click on the code1 activity and choose Breakpoint Insert Breakpoint.
4. You should now see a red circle to the left of the code1 activity.
6. The ConsoleTestHost will start a workflow instance and that instance will break in the debugger when it gets to the code activity. The yellow box indicates the activity that the debugger is stopped at
9. The workflow will now complete and the program finishes.
