15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

OC304 ❘ ChaPTer 57 windOws wOrkflOw fOundAtiOn 3.0<br />

service can then capture the current state of the workflow <strong>and</strong> store it in a persistent storage medium. You<br />

could store the workflow state on disk in a file, or store this data within a database such as SQL Server.<br />

The workflow libraries contain an implementation of the persistence service, which stores data within a<br />

SQL Server database — this is the SqlWorkflowPersistenceService. In order to use this service, you<br />

need to run two scripts against your SQL Server instance. One of these constructs the schema, <strong>and</strong> the other<br />

creates the stored procedures used by the persistence service. These scripts are, by default, located in the<br />

C:\Windows\Microsoft.<strong>NET</strong>\Framework\v3.5\Windows Workflow Foundation\SQL\EN directory.<br />

The scripts to execute against the database are SqlPersistenceProviderSchema.sql <strong>and</strong><br />

SqlPersistenceProviderLogic.sql. These need to be executed in order, with the schema file first <strong>and</strong><br />

then the logic file. The schema for the SQL persistence service contains two tables: InstanceState<br />

<strong>and</strong> CompletedScope. These are essentially opaque tables, <strong>and</strong> they are not intended for use outside the<br />

SQL persistence service.<br />

When a workflow idles, its state is serialized using binary serialization, <strong>and</strong> this data is then inserted into<br />

the InstanceState table. When a workflow is reactivated, the state is read from this row <strong>and</strong> used to<br />

reconstruct the workflow instance. The row is keyed on the workflow instance ID <strong>and</strong> is deleted from the<br />

database once the workflow has completed.<br />

The SQL persistence service can be used by multiple runtimes at the same time — it implements a locking<br />

mechanism so that a workflow is accessible by only one instance of the workflow runtime at a time. When<br />

you have multiple servers all running workflows using the same persistence store, this locking behavior<br />

becomes invaluable.<br />

To see what is added to the persistence store, construct a new workflow project <strong>and</strong> add an instance of the<br />

SqlWorkflowPersistenceService to the runtime. The following code shows an example using declarative<br />

code:<br />

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())<br />

{<br />

workflowRuntime.AddService(<br />

new SqlWorkflowPersistenceService(conn, true, new TimeSpan(1,0,0),<br />

new TimeSpan(0,10,0)));<br />

// Execute a workflow here.<br />

}<br />

If you then construct a workflow that contains a DelayActivity <strong>and</strong> set the delay to something like<br />

10 seconds, you can then view the data stored within the InstanceState table. The 05 WorkflowPersistence<br />

example contains the preceding code <strong>and</strong> executes a delay within a 20-second period.<br />

The parameters passed to the constructor of the persistence service are shown in the following table.<br />

ParameTer desCriPTion tefaulT<br />

ConnectionString<br />

UnloadOnIdle<br />

InstanceOwnershipDuration<br />

LoadingInterval<br />

The database connection string used by the<br />

persistence service.<br />

Determines whether a workflow is unloaded when it<br />

idles. This should always be set to true; otherwise no<br />

persistence will occur.<br />

This defines the length of time that the workflow<br />

instance will be owned by the runtime that has loaded<br />

that workflow.<br />

The interval used when polling the database for<br />

updated persistence records.<br />

None<br />

False<br />

None<br />

2 Minutes<br />

These values can also be defined within the configuration file.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!