Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

29
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe

Transcript of Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

Page 1: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Windows Workflow Foundation

Ruwan Wijesinghe

Page 2: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

2Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Introduction

• Windows Workflow Foundation is a development framework that enables you to embed workflows in .NET Framework applications.

• Windows Workflow Foundation enables model-driven workflow development

• Providing natural design visibility and hiding system-level concerns such as transactions, state management, and concurrency control

• There are two primary facets of programming workflows. • Designing workflows and their activities

• Using workflows within an application

Page 3: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

3Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Workflow Authoring

• Code Only

This is the default authoring mode for Windows Workflow Foundation, which enables you to use C# or Visual Basic code to specify a workflow using the Windows Workflow Foundation API set. Visual Studio IDE generates a visual representation of the workflow from this code, and support visual editing of workflow (similar to WinForms)

• Markup Only

Design the workflow using an XAML based Workflow markup language called XOML.

• Code-Behind

Design the workflow using an XAML based Workflow markup language and develop workflow control code in a separate code file (as a partial class). This is similar to ASP.NET 2.0 programming modal. Visual Studio supports a Visual editing of these workflows

Page 4: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

4Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Types of Workflows

• Sequential workflows• This style is straightforward and useful for repetitive,

predictable operations, such as designing a set of activities to be performed in a prescribed sequence that is always the same.

• These workflows execute activities in a sequential manner until the last activity completes.

• They can have separate “Cancel Handling” and “Fault Handling” workflows as well

• State Machine workflow• These workflows are made up of set of states.• One state is denoted as a start state.• Each state can receive a certain set of events.• Based on an event a transition can be made to another state.• The state machine workflow can have a final state. When a

transition is made to the final state the workflow completes.

Page 5: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

5Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

A Simple Sequential Workflow

• This is a simple workflow diagram generated using Visual Studio 2005 IDE

Page 6: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

6Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Code Only 1

Workflow1.designer.cs

partial class Workflow1{#region Designer generated code [System.Diagnostics.DebuggerNonUserCode] private void InitializeComponent() { this.CanModifyActivities = true; // codeActivity1 this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler(this.PrintHello); // Workflow1 this.Activities.Add(this.codeActivity1); this.Name = "Workflow1"; this.CanModifyActivities = false; }#endregion

private CodeActivity codeActivity1;}

Page 7: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

7Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Code Only 2

Workflow1.cs

public sealed partial class Workflow1: SequentialWorkflowActivity{ public Workflow1() {

InitializeComponent(); }

public void PrintHello(object sender, EventArgs e) { Console.WriteLine("Hello World"); }}

Page 8: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

8Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Code Behind Mode

Workflow1.xoml<SequentialWorkflowActivity x:Class="WorkflowConsoleApplication1.Workflow1" x:Name="Workflow2" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow"> <CodeActivity x:Name="codeActivity1" ExecuteCode="PrintHello" /></SequentialWorkflowActivity>

Workflow1.xoml.csnamespace WorkflowConsoleApplication1{

public partial class Workflow1 : SequentialWorkflowActivity {

public void PrintHello(object sender, EventArgs e){ Console.WriteLine("Hello World"); } }}

Page 9: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

9Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Activities

• Activities are the elemental unit of a workflow

• Windows Workflow Foundation contains a library of standard activities as well as provides the mechanisms for you to create your own.

• Workflow state information

Page 10: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

10Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

CodeActivity Activity

• Execute a method in the Workflow class

• This method must have a normal event handler format

E.g.

private void OnApproved (object sender, EventArgs e) {

}

Page 11: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

11Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

CallExternalMethodActivity Activity

• Used to call methods in custom local services

• The external method is defined using interfaces

• The properties in the workflow class or its activities can be passed as parameters to the external method

• The actual services that implements the interface is defined by the application that host the workflow runtime (using AddService method of the workflow runtime class)

wfRuntime = new WorkflowRuntime();

myWorkflowService = new MyWorkflowService(); wfRuntime.AddService(myWorkflowService);

Page 12: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

12Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

HandleExternalEventActivity Activity

• Suspend the code execution until an external event occurs

• The event arguments can be assigned to the properties of the workflow class, once the event occurs

• The external event is defined using an interface

• This activity requires an “ExternalDataExchangeService” service instance added to the workflow runtime using “AddService” method

• The actual service class that generates the event (that implements the above event interface) must be add to the ExternalDataExchangeService instance using its “AddService” method, by the workflow hosting application.

wfRuntime = new WorkflowRuntime();ExternalDataExchangeService dataService = new

ExternalDataExchangeService();wfRuntime.AddService(dataService);

eventService = new WorkflowEvents();dataService.AddService(eventService);

Page 13: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

13Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Simple Workflow with Branches

GetMarksActivity - CallExternalMethodActivity Interface: IMyService1 Method: int GetMarks() Return value: Marks

DisplayPass - CallExternalMethodActivity Interface: IMyService1 Method: void PrintMessage(string msg) msg: Pass

DisplayFail - CallExternalMethodActivity Interface: IMyService1 Method: void PrintMessage(string msg) msg: Fail

Page 14: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

14Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

IMyServiceInterface

Interface:[System.Workflow.Activities.ExternalDataExchange]interface IMyService1{ int GetMarks(); void PrintMessage(string msg);}

An Implementation:class MyService1: IMyService1{ public int GetMarks(){ return 30; }

public void PrintMessage(string msg){ Console.WriteLine(msg); }}

Page 15: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

15Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Hosting Workflow

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()){ MyService1 myService = new MyService1(); workflowRuntime.AddService(myService);

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow2));

instance.Start();}

Page 16: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

16Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Branch Condition

• IfElseActivity requires a condition to be defined for each of its branch

• These conditions are generally defined using the value of the properties of the workflow class

• Two types of conditions can be used• Declarative Rule Condition –

These conditions can be created using Visual Studio IDE and saved as

an xml file with the extension rules

• Code Condition These conditions are defined as event handling methods

• The branch that does not have any condition assigned will be executed if no condition is met (if you have only two branches, the branch with no condition is the else branch)

Page 17: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

17Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Workflow Class with a property

public partial class Workflow2 : SequentialWorkflowActivity{ private int marks;

public int Marks { get { return marks; } set { marks = value; } }

}

Page 18: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

18Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Defining a Declarative Rule Condition

Page 19: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

19Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Code Condition

• Code condition is defined as a method in the workflow class as follows (CheckMarks method),

public void CheckMarks(object sender, ConditionalEventArgs e){ if (marks > 50) e.Result = true; else e.Result = false;}

Page 20: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

20Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Some useful activities

DelayActivity Delay execution of workflow for a given amount of time

InvokeWebServiceActivity

Invoke a web service method

ListenActivity The first activity of each of the branch of this activity must be an event handling activity. This activity pause the workflow until any one of these event occurs and then continue along the branch correspond to that event

ParallelActivity Execute each branch workflow in parallel

TerminateActivity Terminate the workflow execution

ThrowActivity Throw an exception

TransactionScopeActivity

Execute the sub activities in this activity inside a transaction

WhileActivity Execute the sub activities until the given condition is met

Page 21: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

21Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

State Machine Workflows

• These workflows contains a finite number of states

• Each state can have separate sequential sub workflows for,• State initialization• State finalization• Respond to each predefined set of events

• State Machine workflow stats from the state marked as “Initial State”

• It ends at the state marked as “Completed State” (Note that only an empty state can be marked as a “Completed State”

• These workflows can have separate event handling workflows independent of the state

• Each sequential workflow can have separate “Cancel Handling” and “Fault Handling” Workflows

Page 22: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

22Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

A Simple State Machine Workflow

Page 23: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

23Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

An Event Handling Sub Workflow

Page 24: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

24Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

RuleSets

• PolicyActivity can be used to apply a set of rules (a RuleSet) to the workflow parameters (properties of the workflow class)

• Rules are evaluate as follows

• Start with the list of active rules.

• Find the highest priority rule.

• Evaluate the rule and execute its Then/Else actions as appropriate.

• If the actions of a rule update a field/property that is used by a previous rule in the list (one with a higher priority), reevaluate that previous rule and execute its actions as appropriate. Note that only those rules with a specific dependency are reevaluated.

• Continue the process until all rules in the RuleSet have been evaluated.

Page 25: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

25Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Creating a RuleSet

Page 26: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

26Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Services

• Some of the operations in workflow runtime relies on external services provided by the hosting application of the workflow runtime.

• These services can be added to the runtime using AddService method

• These external services are• Persistence• Tracking• Scheduling (process and thread scheduling)• Transactions

• Workflow runtime creates default services for Scheduling and Transactions if the runtime host did not provide them

• WinFx comes with SQL Server based Persistence (SqlWorkflowPersistenceService) and Tacking (SqlTrackingService) services by default

Page 27: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

27Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

SQL Server based services

• The database to be used by the SQL Server based services can be prepared by executing the SQL scripts in the following folder,

%WinDir%\WinFX\v3.0\Windows Workflow Foundation\SQL\EN

• Both SqlWorkflowPersistenceService and SqlTrackingService services requires the connection strings to this database

Page 28: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

28Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Persistence Service

• Workflow runtime can save its workflow execution states in a persistent storage if a persistence service is provided to it using “AddService” method

• We can make the runtime to save its execution states using “TryUload” method.

• “WorkflowIdled” event of the workflow runtime is a good place to do this.

• We have to save the instance id (in a database or in some other place) in order to reactivated a persisted work flow. This can be obtained using “WorkflowId” property of the workflow instance

• Persisted workflow can be reactivated using its instance id as follows, WorkflowInstance inst = wfRuntime.GetWorkflow(instanceId);

Page 29: Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe.

29Copyright ©2004 Virtusa Corporation | CONFIDENTIAL

Persistence Service (Unloading Example)

void InitWorkflow () { wfRuntime = new WorkflowRuntime(); wfRuntime.WorkflowIdled +=

new EventHandler<WorkflowEventArgs> (wr_WorkflowIdled);

SqlWorkflowPersistenceService persistanceService = new SqlWorkflowPersistenceService (persistDbConnectionString);

wfRuntime.AddService (persistanceService); wfRuntime.StartRuntime ();}

void wr_WorkflowIdled (object sender, WorkflowEventArgs e){ ThreadPool.QueueUserWorkItem (UnloadInstance, e.WorkflowInstance);}

static void UnloadInstance (object workflowInstance){ ((WorkflowInstance) workflowInstance).TryUnload();}