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.

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

The runtime contains six various CreateWorkflow methods that can be used to construct workflow<br />

instances. The runtime also contains methods for reloading a workflow instance <strong>and</strong> enumerating all<br />

running instances.<br />

The runtime also has a number of events that are raised while workflows are executing — such<br />

as WorkflowCreated (raised when a new workflow instance is constructed), WorkflowIdled (raised<br />

when a workflow is awaiting input such as in the expense-processing example shown earlier), <strong>and</strong><br />

WorkflowCompleted (raised when a workflow has finished).<br />

WorKfloW serViCes<br />

A workflow doesn’t exist on its own. As described in the previous section, a workflow is executed within the<br />

WorkflowRuntime, <strong>and</strong> this runtime provides services to running workflows.<br />

A service is any class that may be needed while executing the workflow. Some st<strong>and</strong>ard services are provided<br />

to your workflow by the runtime, <strong>and</strong> you can optionally construct your own services to be consumed by<br />

running workflows.<br />

This section describes two of the st<strong>and</strong>ard services provided by the runtime. It then shows how you can<br />

create your own services <strong>and</strong> some instances of when this is necessary.<br />

When an activity runs, it is passed some contextual information via the ActivityExecutionStatus<br />

parameter of the Execute method:<br />

protected override ActivityExecutionStatus Execute<br />

(ActivityExecutionContext executionContext)<br />

{<br />

.<br />

}<br />

One of the methods available on this context parameter is the GetService method. This can be used as<br />

shown in the following code to access a service attached to the workflow runtime:<br />

protected override ActivityExecutionStatus Execute<br />

(ActivityExecutionContext executionContext)<br />

{<br />

ICustomService myService = executionContext.GetService();<br />

. Do something with the service<br />

}<br />

The services hosted by the runtime are added to the runtime prior to calling the StartRuntime method. An<br />

exception is raised if you attempt to add a service to the runtime once it has been started.<br />

Two methods are available for adding services to the runtime. You can construct the services in code <strong>and</strong><br />

then add them to the runtime by calling the AddService method. Or, you can define services within the<br />

application configuration file, <strong>and</strong> these will be constructed for you <strong>and</strong> added to the runtime.<br />

The following code snippet shows how to add services to the runtime in code — the services added are those<br />

described later in this section:<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 />

workflowRuntime.AddService(new SqlTrackingService(conn));<br />

.<br />

}<br />

Here are constructed instances of the SqlWorkflowPersistenceService, which is used by the runtime to<br />

store workflow state, <strong>and</strong> an instance of the SqlTrackingService, which records the execution events of a<br />

workflow while it runs.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!