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.

Workflows ❘ 1329<br />

Here, an instance of the WorkflowServiceHost class is constructed, <strong>and</strong> the workflow it exposes is<br />

provided by the GetPropertyWorkflow method call. You can use a code based workflow, as we have done<br />

with this example, or an XAML-based workflow. We then add an endpoint <strong>and</strong> specify a basic HTTP<br />

binding, <strong>and</strong> then enable metadata publishing so that a client can point svcutil.exe at this endpoint to<br />

download the service definition. Once that’s done, it is simply a matter of calling Open on the service host<br />

class, <strong>and</strong> we’re then ready to receive incoming calls.<br />

ensuring That a Workflow Can Be Created<br />

The first activity within the workflow is a Receive, <strong>and</strong> it has been defined with the CanCreateInstance<br />

property set to True. This is important, as it notifies the host that, if a caller calls this operation, a new<br />

workflow instance will be constructed based on that call. What is happening under the covers is that<br />

the WorkflowServiceHost is probing the workflow definition looking for Receive activities, <strong>and</strong> it will<br />

construct a service contract based on these activities. It’s also looking for any such activities with True set<br />

for CanCreateInstance, as then it knows what to do when it receives a message.<br />

The sample code contains a client application that creates a new workflow instance by calling<br />

UploadPropertyInformation; it then uploads information about three rooms using the<br />

UploadRoomInformation operation, <strong>and</strong> finally it calls DetailsComplete. This sets the Finished flag<br />

used by the While activity, <strong>and</strong> this completes that workflow.<br />

The workflow code is fairly complex, as there’s a lot going on under the covers. Knowing how it works,<br />

though, is instructive, so we’ll go through some of it here.<br />

The first activity within the workflow is defined as follows:<br />

Variable address = new Variable();<br />

Variable owner = new Variable();<br />

Variable askingPrice = new Variable();<br />

// Initial receive - this kicks off the workflow<br />

Receive receive = new Receive<br />

{<br />

CanCreateInstance = true,<br />

OperationName = "UploadPropertyInformation",<br />

ServiceContractName = XName.Get("IProperty", ns),<br />

Content = new ReceiveParametersContent<br />

{<br />

Parameters =<br />

{<br />

{"address", new OutArgument(address)},<br />

{"owner", new OutArgument(owner)},<br />

{"askingPrice", new OutArgument(askingPrice)}<br />

}<br />

}<br />

};<br />

This Receive has the CanCreateInstance flag set to true, <strong>and</strong> it defines both operation <strong>and</strong><br />

service contract names. The ServiceContractName includes a namespace (in the example set to<br />

http://pro-csharp/) so as to uniquely identify this service. The Content property defines what is<br />

expected to be passed up from the client when the operation is called — in this instance we’re passing<br />

up address, owner, <strong>and</strong> asking price values. The service operation is completely defined here by this<br />

code — <strong>and</strong> the metadata exported uses this code to construct the operation definition.<br />

The arguments received from this operation are bound to variables defined by the workflow. This is how we<br />

pass arguments from the outside world into a workflow hosted by WCF.<br />

Inside the workflow, we then create the unique ID that will be used when calling back to this workflow<br />

instance. In order to do this, we need to define a workflow variable <strong>and</strong> assign its value — in the code that<br />

follows, we’ve used an Assign activity to do this.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!