demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are...

27
WF4 from the Inside Out Bob Schmidt Program Manager Microsoft Corporation FT04

Transcript of demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are...

Page 1: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

WF4 from the Inside Out

Bob SchmidtProgram ManagerMicrosoft Corporation

FT04

Page 2: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

What is this talk about?

> Discuss the central ideas of WF, show how WF works, get you to experiment

> Focus is on runtime (what happens) more than programming model (how you express what you want to happen)

> You won’t find feature lists, best practices, guidance

> You might find ideas, inspiration, insights

Page 3: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Where are we?

Process Host

Application Host

WF Runtime

IIS Management

PowerShell

MonitoringPersistence

Storage

WCFRuntime

Visual Studio

Activities

Page 4: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

IdeasPrograms are data

(opacity begone)

Scheduler-based program execution(stackless & serializable)

Runtime-mediated code rendezvous(thou shalt not block the thread)

Natural control flow(run it the way you picture it)

Page 5: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

WF Programs

demo

Page 6: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Authoring

> A WF program is an activity> Can author programmatically or using XAML – or

in any format from which an activity can be created

> An activity can contain other activities> An activity can have input and output arguments

(and also variables)> Separate base classes for activities that return a

value> e.g. CodeActivity<TResult>, NativeActivity<TResult>

> A WF program is a definition from which many instances can be created> Each instance of an activity has a unique

environment(visible data values: args, vars)> InArgument value is obtained from environment

Page 7: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Execution

> WF runtime just sees activities, activities, activities (not Sequence, Parallel, Recurrence)> WF runtime is the referee, enforces “rules of the

game”> CacheMetadata is how an activity describes

itself> WF runtime knows when an activity is done> An activity can schedule the execution of a

child activity and be notified upon its completion> There can be multiple, distinct method

invocations (“pulses of work”) per activity> Can express all kinds of patterns (control flow)

> Multiple paths of execution…

Page 8: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Scheduler

WorkItem 1WorkItem 2WorkItem 3WorkItem 4

WF Runtime

External Code

Activity Code

while !EmptyList Execute(next WorkItem)

Work items are executed one at a

time within a single workflow instance

POP

Some work items are added to the front of the list, others

to the back

ADD

PUSH

ENQUEUE

while !EmptyList Execute(next WorkItem) DoBookkeeping()

while !EmptyList Execute(next WorkItem) DoBookkeeping()

Page 9: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

PARALLEL

“1”

“2”

“3”

“4”

Parallel.Execute

SEQUENCE1

SEQUENCE2

Sequence1.Execute

Write(“1”).Execute

Sequence1.Continue

Write(“2”).Execute

Sequence1.Continue

Parallel.Continue

Sequence2.Execute

Sequence2.Execute

Write(“3”).Execute

Sequence2.Continue

Write(“4”).Execute

Sequence2.Continue

Parallel.Continue

[ scheduler example ]

Workflow Instance Complete!

Page 10: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Scheduler Details

> Scheduler per program instance> Manages an ordered list of work items

> Work items can be added by activity code and also by host & external code

> Some work items are added at the front, other work items at the back

> Executes work items one at a time> The executing work item is never pre-

empted> Things can happen between work items

e.g. tracking, persistence

Page 11: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Threading Model

> One thread at a time is used to process work items for a program instance> Simplifies the programming model for activities> Thread may differ for work items in the same

instance> WF runtime uses host-provided SynchronizationContext

> See System.Threading.SynchronizationContext> Extensibility point for host thread management

> Standard TLS mechanisms do not apply to WF> Instead: WF Execution Properties - named properties

visible for a part (sub-tree) of a workflow> Attached to / detached from the current environment by

the WF runtime before / after work item invocation

> Activities should not block this thread> WF programs coordinate work

Page 12: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Asynchronous Activities

demo

Page 13: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Asynchronous Activities

WF Runtime

AsyncActivity

I/O code

BeginExecute BeginWrite

EndExecute EndWrite

AsyncWorkCompletionCallback

WorkItem

WorkItem

WorkItemWorkItemWorkItem

Page 14: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Parallel + async activities = true concurrency

Parallel p = new Parallel{ Branches = { new WriteToFile { FileName = "a", Bytes = … }, new WriteToFile { FileName = "b", Bytes = … } }};WorkflowInvoker.Invoke(p);

Page 15: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Bookmarks

> What happens when your programs need to wait (and wait, and wait) for input?> Maybe there are 1000s of instances waiting

> Want a “zero footprint” WaitForInput()> This is a WF bookmark!

> A named resumption point in a WF program> Resumption will schedule an activity’s callback

method> WF runtime mediates resumption – no need for

the instance to be in memory> WCF Receive activity is built on top

Page 16: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Bookmark Resumption

“pizza”

“Please deliver [data] to

instance [id] at bookmark [name]”

WF Runtime

External Code

Activity Code

RESUME

CREATE

Work item 1Work item 2Work item 3Work item 4

ENQUEUE

Page 17: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Input Activity

demo

Page 18: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Persistence

> Persistence lets you pause an instance, save it somewhere & resume it later> A persisted instance has no affinity to a WF host

instance, CLR instance, thread, process, or machine

> Helps with scalability; helps with recovery from failure

> WF program instances are serializable> A CLR stack is not serializable, but in a WF

program a stack only exists transiently, during work item execution

> Sometimes you want a “no persist zone”> Automatic during execution of an async activity

> Details of persistence are deliberately separate from the machinery of the WF runtime

Page 19: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

What’s in a Serialized Instance?

> A serialized instance contains:> Work item list (empty if instance is idle)> Bookmarks> Data (arg and var values)

> Environments for all executing activity instances> Activity instance info (callbacks, execution

props)> Custom data from persistence participants

> Does not contain the workflow definition> A million instances can share the same definition> Management of definitions is a host

responsibility

Page 20: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

[ example: a serialized instance ]

PARALLEL

DateTime dint n

string s

private int index private int index

Page 21: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

[ example: a serialized instance ]

PARALLEL

INPUT

DateTime dint n

string s

private int index private int index

“hello”

3

Bookmark:“x”

Page 22: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Conclusions

> WF is a way of building programs> Use the right control flow & vocabulary

for your situation> Achieve true concurrency where desired> Model arbitrary wait points using bookmarks > Utilize persistence as needed

> Highly flexible & extensible> Authoring formats> Custom activities, control flow> Thread management, persistence, hosting

Page 23: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Resources

> Ask the Experts tonight> Thursday Sessions

> FT13: What's New in WCF 4> FT14: Workflow Services and Windows

Server AppFabric> FT27: Application Server Extensibility

with .NET 4and Windows Server AppFabric

> http://blogs.msdn.com/endpoint/

[email protected]

Page 24: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

YOUR FEEDBACK IS IMPORTANT TO US!

Please fill out session evaluation

forms online atMicrosoftPDC.com

Page 25: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

Learn More On Channel 9

> Expand your PDC experience through Channel 9

> Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses

channel9.msdn.com/learnBuilt by Developers for Developers….

Page 26: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 27: demo while !EmptyList Execute(next WorkItem) while !EmptyList Execute(next WorkItem) Work items are executed one at a time within a single.