본문으로 건너뛰기

Windows Workflow Foundation Examples

Windows Workflow Foundation (WF) is a Microsoft technology that provides a programming model, runtime engine, and tools for building workflow-enabled applications.

It allows developers to create, host, and execute workflows as a series of activities, providing a visual and declarative way to define business processes.

In this tutorial, we will explore the history of Windows Workflow Foundation, its key features, and provide several examples to demonstrate its capabilities.

History of Windows Workflow Foundation

Windows Workflow Foundation was first introduced as part of the .NET Framework 3.0 release in 2006. It was designed to address the need for a unified way to create and manage workflows in Windows-based applications.

Over the years, WF has evolved and improved with each version of the .NET Framework. In the latest version, WF is part of .NET Core and is known as Core WF, making it cross-platform and available for use in a variety of development scenarios.

Key Features of Windows Workflow Foundation

1. Visual Workflow Designer

One of the primary features of WF is the visual workflow designer, which allows developers to create workflows using a drag-and-drop interface. The designer provides a graphical representation of the workflow, making it easy to define the flow of activities and their relationships.

Here is an example of a simple workflow defined using the visual designer:

using System;
using System.Activities;
using System.Activities.Statements;

class Program
{
static void Main()
{
WorkflowInvoker.Invoke(new HelloWorldWorkflow());
}
}

class HelloWorldWorkflow : Activity
{
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("Hello, World!");
}
}

When executed, this workflow will print "Hello, World!" to the console.

2. Activity Model

WF provides a rich set of built-in activities that can be used to define the behavior of workflows. Activities represent units of work within the workflow and can be combined to form complex business processes.

Here is an example of a workflow that uses built-in activities to perform a simple calculation:

using System;
using System.Activities;
using System.Activities.Statements;

class Program
{
static void Main()
{
WorkflowInvoker.Invoke(new CalculatorWorkflow());
}
}

class CalculatorWorkflow : Activity
{
public InArgument<int> Operand1 { get; set; }
public InArgument<int> Operand2 { get; set; }
public OutArgument<int> Result { get; set; }

protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.AddArgument(new RuntimeArgument("Operand1", typeof(int), ArgumentDirection.In));
metadata.AddArgument(new RuntimeArgument("Operand2", typeof(int), ArgumentDirection.In));
metadata.AddArgument(new RuntimeArgument("Result", typeof(int), ArgumentDirection.Out));
}

protected override void Execute(NativeActivityContext context)
{
int operand1 = Operand1.Get(context);
int operand2 = Operand2.Get(context);
int result = operand1 + operand2;
Result.Set(context, result);
}
}

In this example, the workflow takes two input arguments (Operand1 and Operand2) and calculates their sum. The result is stored in the Result output argument.

3. Workflow Hosting

WF provides a runtime engine that allows workflows to be hosted and executed within different types of applications. Workflows can be hosted in console applications, Windows services, ASP.NET applications, or even within other workflows.

Here is an example of hosting a workflow in a console application:

using System;
using System.Activities;
using System.Activities.Hosting;

class Program
{
static void Main()
{
WorkflowInvoker.Invoke(new HelloWorldWorkflow());
}
}

class HelloWorldWorkflow : Activity
{
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("Hello, World!");
}
}

When executed, this console application will host and execute the HelloWorldWorkflow, which will print "Hello, World!" to the console.

Examples of Windows Workflow Foundation

1. Approval Workflow

An approval workflow is a common use case for WF, where a document or request goes through a series of approval steps before it is finalized.

Here is an example of an approval workflow:

using System;
using System.Activities;
using System.Activities.Statements;

class Program
{
static void Main()
{
WorkflowInvoker.Invoke(new ApprovalWorkflow());
}
}

class ApprovalWorkflow : Sequence
{
public Variable<string> Status { get; set; }

public ApprovalWorkflow()
{
Status = new Variable<string>();

Activities.Add(new WriteLine { Text = "Approval Workflow" });
Activities.Add(new Assign<string> { To = Status, Value = "Pending" });
Activities.Add(new If
{
Condition = new VisualBasicValue<bool>("Status = \"Pending\""),
Then = new WriteLine { Text = "Approval Required" },
Else = new WriteLine { Text = "Approval Not Required" }
});
}
}

In this example, the workflow starts by printing "Approval Workflow" to the console. It then assigns the initial status as "Pending" and checks if approval is required based on the status value. If the status is "Pending", it prints "Approval Required"; otherwise, it prints "Approval Not Required".

2. Parallel Workflow

A parallel workflow allows multiple activities to be executed concurrently, improving the overall performance and efficiency of the workflow.

Here is an example of a parallel workflow:

using System;
using System.Activities;
using System.Activities.Statements;

class Program
{
static void Main()
{
WorkflowInvoker.Invoke(new ParallelWorkflow());
}
}

class ParallelWorkflow : Parallel
{
public ParallelWorkflow()
{
Branches.Add(new WriteLine { Text = "Task 1" });
Branches.Add(new WriteLine { Text = "Task 2" });
Branches.Add(new WriteLine { Text = "Task 3" });
}
}

In this example, the parallel workflow executes three tasks concurrently: "Task 1", "Task 2", and "Task 3". The output will show that all three tasks are executed in parallel.

Conclusion

In this tutorial, we have explored the introduction, history, and key features of Windows Workflow Foundation. We have also provided several examples to demonstrate its capabilities. With WF, developers can create powerful and flexible workflow-enabled applications, enhancing the efficiency and manageability of business processes.

For more information and official documentation on Windows Workflow Foundation, you can visit the Microsoft Docs website.