Chapter 1 – Workflow Program

Packet — MS WWF 4.0 Cookbook – 02/16/2011, 6:28PM

Need to think of three fundamental things:
(1) How to define workflow programs
(2) How to build (compile) workflow programs
(3) How to execute workflow programs

in WF4, we can define a workflow in either (1) Managed .NET Code or (2) XAML.

Two kinds of code workflow authoring styles:
(1) Creating a Custom Activities class
(2) Creating workflow dynamically in the runtime

Two ways to author workflow in XAML:
(1) By WF Designer (recommended)
(2) Typing XML tags manually (no way!)

Essentially, a workflow is a .NET program…. After defining workflows, we can build workflow applications as we build normal .NET applications.

HelloWorldWorkflow
Create our first workflow to print “Hello World” to the console application.

In VS 2010, open a new workflow console project, named HelloWorkflow

First drag a sequence activity to the designer (WF Designer), and then a Writeline activity inside. (this is in Workflow1.xaml)

Inside Writeline, enter a VB expression, or a string with quotes.

Run it! Ctrl + F5 without debugging or F5 with Debugging

How it works – in Program.cs:

All XAML files are compiled into DLL or EXE files.
So far there is no WF4 Designer add-ins for VS 2008, so we will need VS 2010.

Creating a WF Program Using C#

Create the same “HellowWorkflow” function workflow using pure C#, beginning from a console application.



Then you have this:

Add a reference to System.Activities
assembly.

Update the code and run with Ctrl + F5

Writeline activity belongs to System.Activities.Statements.

(Implementing a class inherited form Activity)

Initializing a WF program using InArguments —

Create a Workflow console app. “UseInArgument”.
Create 2 arguments; place on Sequence, and two writelines:

Write Code: Provide parameters in the code. Ctrl + F5.

Note: FirstName and LastName are InArgument type.

Creating a WF program using OutArgument

New WorkflowConsole application, “UseOutArg”.

First create an OutMessage argument (OUT)


Drga one sequence, two writelines and one assign

Edit code: invoke the workflow, return a value and save in output. Then print output.

A third kind of workflow argument: InOutArgument.
In most cases, we can use InOutArgument in place of InArgumnet/OutArgumnet.
Buy you cannot assign value to InOutArgument.

Creating a WF Program using InOutArgument

This type of argument is used to receive values and is also used to pass values out to caller. (WF Host)

New Workflow Console App, “UseInOutArgument”.

Using (temp) Variable in a WF Program

New Workflow Console app, “UseVariable”

Add a sequence, create a variable, Int32, NumberCounter, scope=Sequence


Run it…

Running a Workflow Asynchronously —

In previous example, we start a workflow instance on the same thread as the main program. However, in real world, a workflow should run on independent thread.

Create new Workflow Console App, “WorkFlowApplication”.

As the workflow thread runs simultaneously with the caller thread, the caller thread may terminate before the workflow thread. To prevent this, we need to use AutoResetEvent to sync caller and workflow thread.

AutoResetEvent syncEvent = new AutoResetEvent(false);

The caller thread will wait there, until syncEvent is set.
syncEvent.WaitOne();

**** we get dictionary output from the WorkFlowApplicationCompleteEventArgs‘s output property. (p.25) 11:28PM

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: