Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Workflow Foundation Ruwan Wijesinghe."— Presentation transcript:

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

2 2 Copyright ©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

3 3 Copyright ©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

4 4 Copyright ©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.

5 5 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL A Simple Sequential Workflow This is a simple workflow diagram generated using Visual Studio 2005 IDE

6 6 Copyright ©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; }

7 7 Copyright ©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"); }

8 8 Copyright ©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"> Workflow1.xoml.cs namespace WorkflowConsoleApplication1 { public partial class Workflow1 : SequentialWorkflowActivity { public void PrintHello(object sender, EventArgs e){ Console.WriteLine("Hello World"); }

9 9 Copyright ©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

10 10 Copyright ©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) { }

11 11 Copyright ©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);

12 12 Copyright ©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);

13 13 Copyright ©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

14 14 Copyright ©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); }

15 15 Copyright ©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(); }

16 16 Copyright ©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)

17 17 Copyright ©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; } }

18 18 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Defining a Declarative Rule Condition

19 19 Copyright ©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; }

20 20 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Some useful activities DelayActivityDelay execution of workflow for a given amount of time InvokeWebServiceActivityInvoke a web service method ListenActivityThe 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 ParallelActivityExecute each branch workflow in parallel TerminateActivityTerminate the workflow execution ThrowActivityThrow an exception TransactionScopeActivityExecute the sub activities in this activity inside a transaction WhileActivityExecute the sub activities until the given condition is met

21 21 Copyright ©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

22 22 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL A Simple State Machine Workflow

23 23 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL An Event Handling Sub Workflow

24 24 Copyright ©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.

25 25 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Creating a RuleSet

26 26 Copyright ©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

27 27 Copyright ©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

28 28 Copyright ©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);

29 29 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Persistence Service (Unloading Example) void InitWorkflow () { wfRuntime = new WorkflowRuntime(); wfRuntime.WorkflowIdled += new EventHandler (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(); }


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

Similar presentations


Ads by Google