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.

1330 ❘ ChaPTer 44 windOws wOrkflOw fOundAtiOn 4<br />

return new Sequence<br />

{<br />

Variables = { propertyId, operationH<strong>and</strong>le, finished,<br />

address, owner, askingPrice },<br />

Activities =<br />

{<br />

receive,<br />

new WriteLine { Text = "Assigning a unique ID" },<br />

new Assign<br />

{<br />

To = new OutArgument (propertyId),<br />

Value = new InArgument (Guid.NewGuid())<br />

},<br />

new SendReply<br />

{<br />

Request = receive,<br />

Content = SendContent.Create<br />

(new InArgument (env => propertyId.Get(env))),<br />

CorrelationInitializers =<br />

{<br />

new QueryCorrelationInitializer<br />

{<br />

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

MessageQuerySet = extractGuid<br />

}<br />

}<br />

}<br />

// Other activities omitted for clarity<br />

}<br />

};<br />

Next, the SendReply activity is used to respond to the caller with the Guid defined by the workflow. This<br />

part is fairly complex.<br />

The SendReply activity is linked to the initial Receive activity by setting its Request property to the<br />

receive activity instance. The return value of the operation is defined by the Content property. There are<br />

three types of responses that can be returned from an operation — a discrete value, such as a Guid used<br />

here, a set of parameters defined by a dictionary of name/value pairs, or a message type. The SendContent<br />

class is a helper that constructs the appropriate object for you.<br />

The last part is the most complex. The CorrelationInitializers property is effectively used to extract<br />

a correlation token from the current message <strong>and</strong> use this in subsequent calls to the workflow to uniquely<br />

identify the workflow. The MessageQuerySet is used to do the extraction <strong>and</strong> is defined as shown below for<br />

this outgoing message. Once the query has been defined, it is then linked with a h<strong>and</strong>le value. This h<strong>and</strong>le is<br />

used by any other activity wishing to be part of the same correlation group.<br />

MessageQuerySet extractGuid = new MessageQuerySet<br />

{<br />

{ "PropertyId",<br />

new XPathMessageQuery ( "sm:body()/ser:guid", messageContext ) }<br />

};<br />

This query uses XPath (otherwise known as voodoo magic) to extract data from the message being sent<br />

back to the client. Here, it is reading the body element <strong>and</strong>, within that, looking for the value of the guid<br />

element within the message.<br />

awaiting events with Pick<br />

So, we’ve constructed a new workflow instance <strong>and</strong> replied to the caller with a unique operation ID. The<br />

next part is to await further data from the client, which is done with two further Receive activities.<br />

The first of these corresponds to the UploadRoomInformation operation.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!