15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Workflows ❘ 1327<br />

persistence provider to load up an existing instance from the database. This instance is then resumed by<br />

calling the ResumeBookmark function.<br />

If you run the example, you’ll see a prompt onscreen. While that prompt is there the workflow is persisted<br />

<strong>and</strong> unloaded, <strong>and</strong> you can check this by running SQL Server Management Studio <strong>and</strong> executing the<br />

comm<strong>and</strong> shown in Figure 44-13.<br />

figure 44-13<br />

Workflow instances are stored within the InstancesTable of the System.Activities.DurableInstancing<br />

schema. The entry shown in Figure 44-13 is the persisted instance of the workflow running on my machine.<br />

When we continue the workflow it will eventually complete, <strong>and</strong> at that point the workflow will<br />

be deleted from the instances table, as there’s an option on the instance store exposed as the<br />

InstanceCompletionAction, which by default is set to be DeleteAll. This ensures that any data stored<br />

in the database for a given workflow instance will be deleted once that workflow completes. This is a<br />

sensible default, as once a workflow completes you would normally not expect any data to hang around.<br />

You can change this option when you define the instance store by setting the instance completion action to<br />

DeleteNothing.<br />

If you now continue running the test application <strong>and</strong> then retry the SQL comm<strong>and</strong> from Figure 44-13, you<br />

should notice that the workflow instance has been deleted.<br />

Workflowservicehost<br />

A long way back in this chapter we mentioned that there were three ways to host workflows — the last<br />

is by using the WorkflowServiceHost class which exposes a workflow through WCF. One of the major<br />

areas that Workflow is destined to be used for is as the back-end to WCF services. If you think about what<br />

a typical WCF service does, it’s usually a bunch of related methods that are typically called in some sort of<br />

order. The main problem here is that you could call these methods in any order, <strong>and</strong> usually you need to<br />

define the ordering so that, for example, the order details are not uploaded before the order itself.<br />

With Workflow you can easily expose WCF services that also have a notion of method ordering. The<br />

main classes you’ll use here are the Receive <strong>and</strong> the Send activities. In the code for this example (which is<br />

available in the 07_WorkflowsAsServices solution), the scenario used is an estate agent (a Realtor if you’re<br />

in the United States) who wishes to upload information about a property to a web site.<br />

The first call to the service will construct a new workflow instance, <strong>and</strong> this call will return a unique ID<br />

that is used in subsequent calls to the service. We’ll then use this unique ID in subsequent calls, as this will<br />

identify which workflow instance on the server we wish to communicate with. Workflow uses a facility<br />

called “correlation” to map an incoming request to a workflow instance. Here, we’re using message-based<br />

correlation to link a property of the incoming message with an existing workflow instance. The workflow<br />

we’ll use for this example is outlined in Figure 44-14.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!