It works on my machine

Learning WF - Part 6

Let's continue from where we left off, in the current installment we will learn about communicating between host and the workflow instance. Why is this communication important when we can execute any code we want in the workflow? Well, we cannot act on workflow itself to begin with. That's not just it, there are many other scenarios where it is important that we are able to pass information between the hosting program and workflow, for example if the workflow were hosted in a web service. Create a new sequential workflow project called TalkToMe.

 image

Once it is created add new item of the type Interface and call it ICalculator. Communication can take place in two directions, from workflow to host where workflow calls a host function and from host to workflow where host sends the workflow "instance" an event. The key to understanding how communication between host and the workflow works is to understand how workflow communication is designed in general. The services that workflows use from the runtime have actually been separated from the runtime and implemented as independent services which can be added to the runtime and to communicate with these services we need to base these services on interfaces and then use those interfaces to call their functions. Our calculator service will be one such service and we shall now define its interface.

image

Notice the attribute ExternalDataExchange over the interface. This marks the interface usable as runtime service. Next create the implementation for this service right here without adding another file.

 image

Now drop the activity CallExternalMethod and Code to the designer.

image

select the first activity and specify the following details.

 image

The method details are quite obvious except the parameters that we are passing. The IDE has correctly recognized the input values from the selected method and that it has a return value. The input values specified here are static right now but these could be dynamic if we link these values to some value from another activity or link them to properties in the workflow. For the input we used static values but for the return value we must specify some receiving variable. To do that I have defined a property in my workflow and linked it to the return value.

image

This value can now be selected in the return value parameter in properties.

image

We will now use this property in the code activity to print the results.

 image

We can now execute and see the results for it. This covers the scenario where the workflow is communicating with the outside services. How about the outside services talking back to the workflow? To do that we have to define an event and then use a HandleExternalEventActivity to receive the call from outside. Drop this activity from the toolbar to the designer and lets add code to support the callback.

image

Now we have to create the event infrastructure. For that modify the ICalculator interface and add the following event.

 image

I have added an event and a function to raise that event. Notice that I am passing CalcArgs argument to the event handler. To pass any data to the event we need to derive our own class from the ExternalDataEventArgs class.

image

The definition of the event arguments only contains one variable called CalcResult. The constructor points to an important fact about this event handling, which is that the "callback" is bound to a specific instance of the workflow and we need to pass it to the base class here. To acquire this instance ID we need to get it either from within the workflow or where the workflow instance was created. For this example I will get it from the point of creation. In the Program class I have added the following code beside the workflow instance code.

image

After creation of the workflow instance I simply wait for the user to press the enter key and then raise the event to pass information to the workflow instance. Let's look at the implementation code for our event.

image

This goes in the implementation class of the ICalculator interface. Let's go back to our designer to see what have we specified in the properties for the event handler activity.

image

So the activity is receiving an event called CalculationComplete from an ICalculator type class. Notice the "e" parameter, two arguments are passed to any event handler, the sender and the arguments. For the moment we are only interested in receiving the arguments, so for that we create a property in our workflow class for it.

image

And assign this to the "e" parameter.  once the event is called we can get the CalcResult value, which if you look back is 10, and use it other activities.

This concludes the post on host to workflow communication. In the next post we will look at creating state machine workflow and the things that we have learnt here is necessary background for it.

Posted: May 10 2008, 21:32 by raza | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: .Net | WF
blog comments powered by Disqus