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 ❘ 1331<br />

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

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

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

// Receive room information<br />

Receive receiveRoomInfo = new Receive<br />

{<br />

OperationName = "UploadRoomInformation",<br />

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

CorrelatesWith = operationH<strong>and</strong>le,<br />

CorrelatesOn = extractGuidFromUploadRoomInformation,<br />

Content = new ReceiveParametersContent<br />

{<br />

Parameters =<br />

{<br />

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

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

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

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

}<br />

}<br />

};<br />

The variables initially defined are used to record the data passed from the client to the workflow. Again<br />

the Receive activity specifies both the OperationName <strong>and</strong> the ServiceContractName so that metadata<br />

can be generated for this operation. The CorrelatesWith value is critical here, as it links the different<br />

operations of the workflow together. In the SendReply activity, we created this h<strong>and</strong>le so that we could<br />

reference it with these later operations. Without this h<strong>and</strong>le, it would not be possible to work out which<br />

workflow instance an incoming message is for.<br />

The CorrelatesOn property defines what is extracted from the incoming message in order to verify that the<br />

message is for this workflow instance. Again, a MessageQuerySet is used, which in this case extracts<br />

the unique ID from the incoming message:<br />

MessageQuerySet extractGuidFromUploadRoomInformation =<br />

new MessageQuerySet<br />

{<br />

{ "PropertyId",<br />

new XPathMessageQuery<br />

( @"sm:body()/local:UploadRoomInformation/local:propertyId",<br />

messageContext ) }<br />

};<br />

Here, the XPath is looking for the propertyId element within the UploadRoomInformation call.<br />

The last part of this Receive defines what happens with the incoming parameters passed to the service<br />

operation. Here, I’m expecting the unique propertyId, a room name <strong>and</strong> dimensions. This data is<br />

extracted into variables, <strong>and</strong> in the example code we simply output it to the console. In a real application,<br />

you would obviously use this data to update a database or something similar.<br />

The last Receive activity is used to await the DetailsComplete message from the client. It follows a similar<br />

pattern to the other receive activities in that it defines the service contract name <strong>and</strong> operation name, uses the<br />

same correlation h<strong>and</strong>le as the initial Send <strong>and</strong> the Receive used for the UploadRoomInformation operation,<br />

<strong>and</strong> it uses a MessageQuerySet in order to retrieve the unique property ID from the incoming message.<br />

With those activities in place, the rest of the workflow is a While loop that contains the Pick activity <strong>and</strong><br />

its branches:<br />

new While<br />

{<br />

Condition = ExpressionServices.Convert<br />

(env => !finished.Get(env)),<br />

Body = new Pick<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!