windows workflow foundation (wf)

Upload: cao-xuan-duy

Post on 10-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Windows Workflow Foundation (WF)

    1/40

    Adam Cogan & Mark Liu

    Windows Workflow Foundation(WF)

  • 8/8/2019 Windows Workflow Foundation (WF)

    2/40

    About Mark

    Senior Software Architect at SSW

    Specialises in

    ASP.NET 2.0

    Windows Workflow

    Windows Communication Foundation

    NHibernate / NetTiers / LINQ

    SQL Server 2005 (Reporting Service,Integration Service)

  • 8/8/2019 Windows Workflow Foundation (WF)

    3/40

    Agenda

    Part I - Introduction to Windows WorkflowFoundation What is Workflow? What is Windows Workflow Foundation?

    Workflow Types

    Part II Workflow Architecture Hosting architecture Built-in Activities

    Built-in Services Workflow Persistence Workflow Tracking

  • 8/8/2019 Windows Workflow Foundation (WF)

    4/40

    What is Workflow?

    Workflow is the operational aspect of awork procedure: how tasks are structured,who performs them, what their relative

    order is, how they are synchronized, howinformation flows to support the tasks andhow tasks are tracked.

    Source: http://en.wikipedia.org/wiki/Workflow

  • 8/8/2019 Windows Workflow Foundation (WF)

    5/40

    What is Workflow?

    It is a series of steps that makes up abusiness process

    It can involve people I.e. Wait For Approval, Notify Manager

    It can co-ordinate software I.e. Check Inventory, Debit Account

    It organise them into flowchart or statediagram

    It can be short lived I.e. page control, automation It can be long lived

    I.e. online auction, approval process

  • 8/8/2019 Windows Workflow Foundation (WF)

    6/40

    Introduction to WindowsWorkflow Foundation

    It is a framework for rapid development ofworkflow-enabled application

    Extensible Programming model

    Visual Designer Tool With Visual Studio 2005 and 2008

    Workflow Engine In-process workflow engine to support different

    architecture scenario

  • 8/8/2019 Windows Workflow Foundation (WF)

    7/40

    Key concepts

    Workflow is a set ofActivitiesWorkflows run within a HostProcess: any application orserver

    Developers can build theirown Custom Activity Libraries Base Activity Library: Out-of-box activities and base forcustom activities

    Runtime Engine: Workflowexecution and state management

    Runtime Services: Hostingflexibility and communication

    Visual Designer: Graphicaland code-based construction

    Windows Workflow Foundation

    Base Activities Library

    Custom Activity Library

    Runtime Engine

    Runtime Services

    A Workflow

    An Activity

    Visual Designer

    Host Process

  • 8/8/2019 Windows Workflow Foundation (WF)

    8/40

    Custom Application

    How does it hang together?

    Add customworkflows

    Add customre-usable activities

    Add customservices tocommunicate

    other systems Host workflow with

    Workflow Runtime

    Services

    Workflow Runtime

    Activities

    Runtime

    Services

    Custom

    Services

    Built-inActivities

    CustomActivities

    Custom Workflows

  • 8/8/2019 Windows Workflow Foundation (WF)

    9/40

    Workflow Types

    Sequential Workflow

    Looks like a flow chart

    State Machine Workflow Looks like a graph

  • 8/8/2019 Windows Workflow Foundation (WF)

    10/40

    Sequential Workflow

    Characteristics Start from the first activity

    Execute in sequence

    Limitation Cannot go back to previous activity

    without adding Loop

    Cannot have two go-back loop thatintersect each other

    Inflexible when interacting with

    external system, where event ordercannot be guaranteed

    Scenario Automation, Document Processing

  • 8/8/2019 Windows Workflow Foundation (WF)

    11/40

    Demo: Simple Workflow

    Create a Simple Workflow

    Create Workflow with Code Activity

    Create a Console Application

    Create Workflow Runtime

    Create HelloWorldWorkflow Instance

    Start the Workflow

  • 8/8/2019 Windows Workflow Foundation (WF)

    12/40

    State Machine Workflow

    Characteristics Event driven

    System to system,

    Human to system (via application)

    Execution based on current State More flexible for external changes

    Start with an Initial State and finishes when it reachesCompleted State

    Limitation

    Cannot create re-usable state activities Designer is buggy with Debugger

    Scenario Approval workflow with multiple approval level

  • 8/8/2019 Windows Workflow Foundation (WF)

    13/40

    Part II Workflow Architecture

  • 8/8/2019 Windows Workflow Foundation (WF)

    14/40

    Hosting Workflow

    Workflows are managed and executed byWorkflowRuntime

    WorkflowRuntime can be hosted by any

    .NET processes Console

    Windows Forms

    ASP.NET

    Windows Service

  • 8/8/2019 Windows Workflow Foundation (WF)

    15/40

    Workflow Runtime

    Runtime Engine The core layer of the Workflow Runtime Responsible for execution and lifecycle of workflow

    Runtime Services

    Each service provides a feature that interact with systemoutside the workflow Runtime Services provide features that interact with the

    Runtime EngineI.e.

    Persistence Tracking Scheduling

    Built-in runtime services can be overwrite by customruntime servicesI.e.

    Custom OleDbPersistenceService that implements IPersistenceService tostore workflow state into any OleDB database.

  • 8/8/2019 Windows Workflow Foundation (WF)

    16/40

    Activities

    Building blocks of workflow Represent unit of execution, re-use and composition

    Basic activities are steps within a workflow

    Composite activities contain other activities

    Activities are classes Properties and events are defined by activity author and

    programmable from the workflow

    Inherit from System.Workflow.ComponentModel.Activityclass

    Contain methods that are invoked by the WorkflowRuntime (E.g. Execute)

    Can be built into workflow assembly or deployed as re-usable libraries

    Best Practice: keep workflow and activities in separate projects

  • 8/8/2019 Windows Workflow Foundation (WF)

    17/40

    Activities

    Activities can have conditions

    Code condition A specific method, created by Activity Author, that returns

    true or false

    The code condition is compiled into the assembly

    Declarative condition A single declarative condition that results in true or false

    The declarative condition is interpreted and is allowed to be

    modified after compilation Policy condition

    A rule set in a policy derived activity consisting of acollection of conditions and resultant actions

  • 8/8/2019 Windows Workflow Foundation (WF)

    18/40

    Built-in Activities

    Control flow activities

    Sequence

    Parallel

    IfElse

    While

    ConditionedActivityGroup

    Replicator

  • 8/8/2019 Windows Workflow Foundation (WF)

    19/40

  • 8/8/2019 Windows Workflow Foundation (WF)

    20/40

    Built-in Activities

    Lifetime activities

    InvokeWorkflow

    Suspend

    Terminate

    Event waiting activities

    EventDriven

    Listen

    Delay

    HandleExternalEvent

  • 8/8/2019 Windows Workflow Foundation (WF)

    21/40

    Built-in Activities

    Web service activities

    Invoke webservice

    WebServiceRequest

    WebServiceResponse

    Other

    InvokeExternalMethod

  • 8/8/2019 Windows Workflow Foundation (WF)

    22/40

    Demo: Building CustomActivities

    Build a ConsoleWrite activity

    Update Hello World workflow

  • 8/8/2019 Windows Workflow Foundation (WF)

    23/40

    Transaction in WindowsWorkflow

    Transaction:

    Atomic (ACID)

    Behave like System.Transactions.TransactionScope

    Cannot be nested

    Exception handlers not permittedLong-Running activities

    May contain atomic or other Long-Running Processes

    May be nested to arbitrary depths

  • 8/8/2019 Windows Workflow Foundation (WF)

    24/40

    Introducing CompensatingLogic

    Transaction is not suited for long runningprocesses. You cannot lock a table for 3 days!

    Need a mechanism to reset committedinformation when a problem is encountered

    For example

    Need to return inventory to stock when customercancel order

  • 8/8/2019 Windows Workflow Foundation (WF)

    25/40

    Activities with CompensationLogic

    Long-running workflow

    Atomic Transaction

    Get Item

    De-queueItem

    Queue DB

    Queue DB

    Compensation Handler

    QueueItem Queue DB

    Normal Execution

    Compensating LogicExecuted only if error occursafter the related transaction

    was properly submitted

  • 8/8/2019 Windows Workflow Foundation (WF)

    26/40

    Compensating Flow

    Long-running workflow

    Atomic Transaction 1 Compensation Handler 1

    Re-queue Item DB

    Atomic Transaction 2

    Process Item DB

    Get Item

    De-queue Item

    DB

    DB

    Compensation Handler 2

    Cleanup Item DB

    If transaction 1 commits and transaction 2 fails then compensation handler 1 will be executed

    The item will be re-queued into the database

  • 8/8/2019 Windows Workflow Foundation (WF)

    27/40

    Runtime Services

    Workflow Loader Service Creates workflow instance based on class type or XML

    definition

    Built-in: Default Workflow Loader Service

    Workflow Scheduler Service Allocate thread to the Runtime Environment to execute

    the workflow

    Built-in: Default Scheduler Service The default scheduler service created by Runtime if none is specified

    Multi-threaded create and manages its own thread pool to execute

    workflows Used in Windows Form, Console and Windows Service apps

    Built-in: Manual Scheduler Service Single threaded only use the thread donated by the caller

    Used in ASP.NET apps

  • 8/8/2019 Windows Workflow Foundation (WF)

    28/40

    Runtime Services

    Persistence Service Stores and loads workflow state into durable storage

    Built in: SqlWorkflowPersistenceService Store workflow state in SQL Server

    Database scripts can be found in:C:\Windows\Microsoft.NET\Framework\v3.0\Windows WorkflowFoundation\SQL\EN

    Tracking Service Track state of the workflow

    Track execution history

    Built in: SqlTrackingService Support customizable tracking criteria

    Store tracking history in SQL Server

    Supports customizable tracking

    See above for database scripts

  • 8/8/2019 Windows Workflow Foundation (WF)

    29/40

    Runtime Services

    WorkflowCommitWorkBatchService

    DefaultWorkflowCommitWorkBatchService Uses MSDTC

    SharedConnectionWorkflowCommitWorkBatchService

    Tracking and Persistence schema must be on the samedatabase

    Avoid MSDTC by sharing database connection between

    persistence and tracking

  • 8/8/2019 Windows Workflow Foundation (WF)

    30/40

    Workflow Persistence

    Saving and loading workflow state from adurable state Built-in SQL Server support

    Sample code on persisting to text files Supports long-running processes

    Serializing workflow is expensive

    Workflow Runtime only persist when

    Transaction Scope completes Workflow is unloaded

    Workflow idles past the configured time

  • 8/8/2019 Windows Workflow Foundation (WF)

    31/40

    Demo: ConfiguringPersistence Service

    Configure database

    Configure Workflow Runtime

    Demonstrate persistence on:

    Transaction

    Idle

    Unload

  • 8/8/2019 Windows Workflow Foundation (WF)

    32/40

    Workflow Tracking

    Generic API that allow applicationdevelopers to track:

    Execution status of the workflow / activities

    Properties in the workflow / activities User-defined track point inside the workflow

    Tracking are filtered by a Tracking Profile

  • 8/8/2019 Windows Workflow Foundation (WF)

    33/40

    Workflow Tracking

    Built-in SqlTrackingService provides:

    Store tracking data to SQL server

    Customizable Tracking Profile by WorkflowType No built-in designer

    Query objects to retrieve tracking data

    Limitation of SqlTrackingService:

    Stores data in an highly normalized and generic schema,difficult to get a specific value for a workflow

    Lack of built-in designer to create custom trackingprofile

    Lack of built-in tools to view the tracked data

    There are sample code that does both

  • 8/8/2019 Windows Workflow Foundation (WF)

    34/40

    Demo: Building ConsoleTracking Service

    Create tracking profile in code

    Write tracking record to console window

  • 8/8/2019 Windows Workflow Foundation (WF)

    35/40

    Demo: Configuring SQLTracking Service

    Setup Sql Tracking Service

    Review result with wfsample Monitor tool

  • 8/8/2019 Windows Workflow Foundation (WF)

    36/40

    Communicating with Host

    Host cannot interact directly with the workflow ExternalDataExchangeService is a runtime service that

    simplifies communication between workflow and host Communication from host-to-workflow works by:

    Host process raises an event defined in a service object

    Runtime passes the event arguments to the correct Workflow Event Arguments must inherit

    System.Workflow.Activities.ExternalDataEventArgs Event must be declared on an interface marked with

    System.Workflow.Activities.ExternalDataExchangeAttribute User-defined service object must implement the said interface

    Communication from workflow-to-host works by: Workflow request a service object from Workflow Runtime that

    implements a specific Interface type Workflow invoke a method directly on the service object

  • 8/8/2019 Windows Workflow Foundation (WF)

    37/40

    Demo: BuildingCommunication Service

    Add events to Console Service

    Consume events from Workflow

  • 8/8/2019 Windows Workflow Foundation (WF)

    38/40

    Demo: State MachineWorkflow

    case-study of a simple approval system

  • 8/8/2019 Windows Workflow Foundation (WF)

    39/40

    Contact Details

    [email protected]

    [email protected]

  • 8/8/2019 Windows Workflow Foundation (WF)

    40/40