In software development, the main goal is to solve real business problems. A workflow is a sequence of ordered steps to realize in order to accomplish a goal, according to a set of rules.
Windows Workflow Foundation (WF) gives a framework for developing workflows in (.Net). In WF, a workflow can be defined in three different ways: with C# code; in XAML (eXtensible Application Markup Language, a declarative language used in Windows Framework); partially in C# code and in XAML. To develop workflows in XAML we can use the WF editor or we can explicitly write the XML tags (XAML files are valid XML files).
Wokflows in WF
The primary building block in Windows Workflow is the activity. All activities in WF derive from an Activity base class. Activities compose the steps, or tasks in a workflow, and define the workflow. We can arrange activities into a hierarchy and feed the activities to the workflow engine as instructions to execute.
Creating the first workflow in WF: Hello Workflow
This workflow is a simple one which only print "Hello Workflow" to the console application.
Using Visual Studio 2010, we start by selecting File -> New Project -> C# section -> Workflow-> Workflow Console Application to create a new WF project. We can name the project HelloWorkflow.
First, drag a WriteLine activity to the designer from the Toolbox. Then, input "Hello Workflow" in the expression box of the WriteLine activity. We can see in the following screenshot:
When we run the project, the main method in the Program.cs file will execute
WorkflowInvoker.Invoke(new Workflow1());
which starts the workflow. WorkflowInvoker provides a means for invoking a workflow. The WorkflowInvoker.Invoke method is synchronous and invokes the workflow on the same thread as the caller. After the workflow starts running, the WriteLine activity prints the "Hello Workflow" to the Console Application.
WorkflowInvoker.Invoke(new Workflow1());
which starts the workflow. WorkflowInvoker provides a means for invoking a workflow. The WorkflowInvoker.Invoke method is synchronous and invokes the workflow on the same thread as the caller. After the workflow starts running, the WriteLine activity prints the "Hello Workflow" to the Console Application.
Creating the first workflow in WF using C# code: Hello WorkFlow
For creating a workflow using C# code and using Visual Studio 2010, we start by selecting File -> New Project -> C# section -> Console Application to create a new project. It is necessary to add a reference to the System.Activities assembly for using, for instance, the WriteLine activity.
Open Program.cs file and add to the used namespaces, the System.Activities and System.Activities.Statements namespaces. System.Activities namespace will we needed for instance, for using the WorkflowInvoker class and System.Activities.Statements namespace will be needed, for instance, for using the WriteLine class.
Change the Program.cs file to:
using System.Activities;
using System.Activities.Statements;
namespace HelloCodeWorkflow
{
class Program
{
static void Main(string[] args)
{
Activity wf = new WriteLine() { Text = "Hello World." };
WorkflowInvoker.Invoke(wf);
}
using System.Activities.Statements;
namespace HelloCodeWorkflow
{
class Program
{
static void Main(string[] args)
{
Activity wf = new WriteLine() { Text = "Hello World." };
WorkflowInvoker.Invoke(wf);
}
}
}
The WorkflowInvoker will invoke the defined activity wf and Text is a property of this activity.

No comments:
Post a Comment