Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Visual Studio 2008: Windows Workflow Foundation

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

Visual Studio® 2008:

Windows® Workflow
Foundation
Module 3: Communicating with Workflows
• Calling Methods on the Host Process

• Handling Events from the Host Process

• Consuming Services from a Workflow

• Publishing a Workflow as a Service


Lesson: Calling Methods on the Host Process
• Overview of Calling Methods on the Host Process

• How to Call Methods on the Host Process

• Generating Strictly Bound Activities for Host Methods

• Demonstration: Calling Methods on the Host Process


Overview of Calling Methods on the Host Process
Windows Workflow Foundation enables a workflow to call
a method on the host process
• Invoke functionality on the host process
• Send data synchronously to the host process

Host Application

Workflow Runtime

ExternalDataExchangeService

Workflow Instance

CallExternal
MethodActivity
How to Call Methods on the Host Process
To make host process methods available to workflows:
• Define an interface that specifies available methods
• Specify the ExternalDataExchange attribute on the interface
• Define a service class that implements the interface
• Register a service class instance with the workflow runtime

To call a host process method from a workflow:


• Add a CallExternalMethodActivity to the workflow
• Specify the interface type and method name to be called
Generating Strictly Bound Activities for Host Methods
What is a strictly bound activity for a host method?
• A subclass of CallExternalMethodActivity
• Contains code to invoke a specific method on an interface

To generate strictly bound activities for host methods:

• Use the workflow communication activity generator


command-line utility, wca.exe

Why use strictly bound activities to call host methods?


• Better designer experience than using CallExternalMethod
• Improved performance by removing the use of reflection
• Makes it easier to customize external interfaces in future
Demonstration: Calling Methods on the Host
Process
In this demonstration you will see how to:
• Define and implement an interface that defines methods
that can be invoked from a workflow
• Add a local communication service to the workflow
runtime engine, to enable the workflow to invoke methods
• Add a CallExternalMethod activity to a workflow, to
invoke a method on the host process
Lesson: Handling Events from the Host Process
• Overview of Handling Events from the Host Process

• Raising Events in the Host Process

• Handling Events in a Workflow

• Generating Strictly Bound Activities for Host Events

• Demonstration: Handling Events from the Host Process


Overview of Handling Events from the
Host Process
Windows Workflow Foundation enables a workflow to
handle events raised in the host process
• Trigger functionality in the workflow
• Send data asynchronously to the workflow

Host Application
• TODO: Redraw the right-hand
Workflow Runtime
side of this diagram, to show
event handling ExternalDataExchangeService

Workflow Instance
Listen Activity
EventDrivenActivity
HandleExternal
EventActivity
Raising Events in the Host Process
To make host process events available to workflows:
• Define an interface that specifies events
• Specify the ExternalDataExchange attribute on the interface
• Define a service class that implements the interface and
raises events
• Register a service class instance with the workflow runtime

The service to workflow relationship is one-to-many


• Many workflows call into a singleton
ExternalDataExchangeMethod instance
• Multiple workflows can have multiple conversations with
the host process through the service
• Inbound workflow calls must be directed to the correct
conversation in the correct workflow
Handling Events in a Workflow
To handle host process events in a workflow:
• Add a HandleExternalEventActivity to the workflow
• Specify the interface type and the event to be handled

Use the ParameterBinding object to pass data


• Used in the same way as when calling methods on the host process
from the workflow

You can use the ListenEvent to wait for events


• Workflow execution is blocked until the required event is raised
• ListenActivity contains HandleExternalEventActivity
activities in parallel branches
• Can wait on multiple events until one is raised
Generating Strictly Bound Activities for
Host Events
What is a strictly bound activity for a host event?
• A subclass of HandleExternalEventActivity
• Contains code to handle a specific event in an interface

To generate strictly bound activities for host events:

• Use the workflow communication activity generator


command-line utility, wca.exe

Why use strictly bound activities to handle host events?


• Better designer experience than HandleExternalEvent
• Improved performance by removing the use of reflection
• Makes it easier to customize external interfaces in future
Demonstration: Handling Events from the Host
Process
In this demonstration you will see how to:
• Define and implement an interface that defines events
that can be handled by a workflow
• Add a local communication service to the workflow
runtime engine, to enable the workflow to handle events
• Add a HandleExternalEvent activity to a workflow, to
handle events raised by the host process
Lesson: Consuming Services from a Workflow
• Overview of WCF Services

• Defining a WCF Service

• Invoking a WCF Service from a Workflow

• Binding Input and Output Parameters

• Demonstration: Consuming Services from a Workflow


Overview of WCF Services
TODO: Add
•Windows information and Foundation
Communication a diagram that explains
(WCF) what WCF
provides a is,
and how
unified it relates to WF
architecture to implement services

A Windows Workflow Foundation workflow can call WCF


services to consume functionality
Workflow WCF Service

Generated
proxy
Defining a WCF Service
Create a WCF Service Library and define a WCF service as
follows:
• Define a service interface and annotate with ServiceContract
• Annotate the interface methods with OperationContract
• Annotate parameter and return data types with DataContract
• Define a class that implements the service interface

[ServiceContract]
public interface IProductsService

  [OperationContract]
  ProductInfo GetProduct(int ID);
} [DataContract]
public class ProductInfo

public class ProductsService :   [DataMember]
IProductsService public string Name...
{...}
Invoking a WCF Service from a Workflow
To invoke a WCF service from a workflow:
• Add a reference to the WCF service in the workflow project
• Add a SendActivity to the workflow
• Set the ServiceOperationInfo property to an operation on
the WCF service
• Create a ChannelToken to specify the endpoint and owner activity
Binding Input and Output Parameters
To bind input and output
properties to a
SendActivity:
• Configure the
ServiceOperationInfo
property
• Visual Studio adds
required properties to
the SendActivity
Properties list
• Click a property, and
then right-click the
activity to display the
binding wizard
• Select the locally
available property from
the list
Demonstration: Consuming Services from
a Workflow
In this demonstration, you will see how to:
• Define a WCF service containing a method that takes
parameters and returns a value
• Add a SendActivity to a workflow to consume a service
method
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Publishing a Workflow as a Service
• Why Publish a Workflow as a Service?

• Defining a Service Contract

• Associating an Activity with a Service Contract

• Implementing Service Behavior in an Activity

• Invoking the Service

• Demonstration: Publishing a Workflow as a Service


Why Publish a Workflow as a Service?
Publishing a workflow as a service enables:
• Workflow logic functionality at the service level
• Granularity of business logic

Client Workflow
application published as a
WCF Service

Generated
proxy
Defining a Service Contract
Create a class library and define a WCF service as follows:
• Define a service interface and annotate with ServiceContract
• Annotate the interface methods with OperationContract
• Annotate parameter and return data types with DataContract

[ServiceContract]
public interface IBasketService

  [OperationContract]
  void ProcessBasket(Basket b);
}

[DataContract]
public class Basket

  [DataMember]
public int NumItems...
Associating an Activity with a Service Contract
To associate an activity with a service contract:
• Add a reference to the class library
• Add a ReceiveActivity to a workflow
• Set the ServiceOperationInfo property to an operation on
the service
• Bind input and output parameters to fields in the workflow
Implementing Service Behavior in an Activity
To implement service behavior
in an activity

• Add child activities to a


ReceiveActivity
Invoking the Service
To invoke the service:
• Add a reference to the service contract assembly
• Add a reference to the workflow assembly

// Create a proxy object for the service workflow.


BasketServiceClient client = new BasketServiceClient();

// Invoke the method on the service.


// This will start a workflow instance implicitly.
decimal basketTotal = client.ProcessBasket();
Console.WriteLine("Basket total: {0:c}", basketTotal);

// Close the client.


client.Close();
Demonstration: Publishing a Workflow as a Service
In this demonstration, you will see how to:
• Publish a workflow as a service

• Invoke a method on the published service


Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lab: Communicating With Workflows

• Exercise 1: Invoking a Method on the Host Process

• Exercise 2: Handling Events from the Host Process

• Exercise 3: Consuming Services from a Workflow

• Exercise 4: Publishing a Workflow as a Service


(If Time Permits)

Logon information

Virtual machine 6462A-LON-DEV-03

User name Student

Password Pa$$w0rd

Estimated time: 60 minutes


Module Review and Takeaways
• Review Questions

• Best Practices

• Tools

You might also like