Using data driven workflow activities and repeated correlation

by Matt Milner 21. February 2006 06:11
How's that for a title?  The driver for this post is that I'm training a lot of folks right now on using WF to build workflows, and when we cover correlation, the example we use has two branches in a parallel activity where we correlate within each branch.  The question inevitably comes up about how to do this dynamically, where the number of "branches" is driven by data.  So, I whipped up this example that uses code from the Microsoft Windows Workflow Foundation Labs (Beta 2) to showcase the Replicator Activity and the Conditioned Activity Group (CAG).  These two activities provide for richer models of business rule or data driven replication using correlation. 
 
In order to enable my example, I have created a composite activity that wraps up the voting behavior found in the communications lab.  This gives me a reusable component that I can then use in multiple places in my workflow, and it also gives me the ability to set properties on the set of activities (now my single composite activity) for each instance.  For example, one of the key properties I can set is the alias, or name, of the user who should vote.  In addition, I can define events at the composite activity that my workflow can listen for and handle.  This allows my workflow to take action when a voter approves or rejects a ballot. Finally, I define a correlation token within the activity and set the parent activity to the composite activity itself.  This provides the context in which my correlation token is valid and is the key to allowing me to correlate within each activity.  Note that it is not required that you do this in a composite activity, the parent activity for your correlation token can be a sequence or some other container.  Finally, I added a property to my workflow called Aliases of type List<string>  which holds the data for my workflow.  I pass in this data from program.cs via parameters to the workflow. 
 
In my workflow, I have two main activities, the first is a replicator which shows how to use this activity to do data driven correlated activities, and the second is the Conditioned Activity Group which allows you to use rules/conditions to determine what branches of activities to run, and when I am done running them. 
 
The replicator activity in my example has the InitialData property bound to the workflow property for aliases, so I am going to run whatever activities are in the replicator as many times as I have items of data.  I also need to pass that data to each instance of the voting activity before it is executed (specifically, the alias it should use).  I use the Child_init event on the replicator to set the alias based on the current data item.  The current data item lets me get the current item in my collection of data and I can then use that to configure the currently executing activity or set of activities.  I also set the execution type property on my replicator to indicate if I want all the branches to run in parallel, or in sequence.  Very powerful option!
 
For the CAG, I have included two voting activities, and bound their aliases to two different instances of input data on the workflow.  I've done this using indexed values from the List<string> property.  When dealing with a CAG, you have to set the "when" property on each branch of execution which indicates the rule or code to determine if the particular branch should execute.  On the CAG itself, you provide a "Until" property that likewise specifies code or rules, but these provide the condition under which execution should stop.  So, the CAG will continue running (foreach/while style looping) until the "Until" condition is met. On each interation, it will use the "when" condition on each activity branch to determine whether it should run that branch.  So you get replication, but with rules to guide it.  You get a lot more than that, but this is a simple example. 
 
When you run the program, it might help to disable one activity and focus on the output by the other.  Running the replicator will give you three vote dialogs, each with the appropriate users name, and the console will appropriately log their votes.  We correlate the response back into the workflow on each instance of the votingactivity.  When you run the CAG, you'll see that the first time through, both voting activities will run.  Until you vote "yes" on one of the items, the CAG will continue to run and use the "when" condition to determine which activity branches to run.  Play around with your voting and notice what happens if you continue to vote no with one person for a few rounds, then vote yes.
 
The final thing to notice is that we have a need to catch the situation, with our CAG, where someone voted yes, but the other person has not yet responded.  If we don't deal with this scenario, when the second person votes, they will get an exception.  We use the WorkflowQueueInfo data from the workflow instance to query the workflow and see if it is waiting for any responses.  If it is not, then we gracefully exist instead of trying to raise the event.  If it is, then we go ahead and raise the event.  This is one way you can attempt to avoid the EventDeliveryFailedException. 
 
Hopefully these examples will prove useful for someone.  I plan to do my best at creating more examples to share.  As always, feedback is welcomed.  You can get the code here
 

Tags:

Windows Workflow Foundation

One Solution For Handling Base Addresses in the Jan CTP of WCF

by Matt Milner 21. February 2006 06:11
One of the changes that occurred on the way to the Jan CTP of WCF was that you must supply a base address to the service host class when constructing it in order to get a metadata access point.  You now use the base address to access the meta data documentation page and can't just use an absolute address in the configuration file and get metadata.  Note that you can still use an absolute address in the configuration file, you just don't automatically get a documentation/metadata functionality if you don't use the base address as well. 
The problem with this is that all of the examples from Microsoft using this new requirement show reading these base addresses from the appSettings in configuration files. This is for good reason.  If anything is going to change in your addressing as you move from one environment to the next, it is probably the base address where you include server names, so it makes sense to store this information in the configuration file so that it can be easily updated without having to recompile the service code.  Being a lazy programmer, I don't want to have to write the same configuration code over and over, so I created a service host base class that does it for me. 
My example, which can be found here, defines the service host derived class that you can use to host your services along with the appropriate configuration code to allow for a custom configuration section that contains the base addresses for your service. The service host base class overrides the OnApplyConfiguration method and adds the addresses from the configuration file into the collection of base addresses for you. 
Feel free to try it out and provide any feedback.  I don't know what the final plans are from MS on this, but hopefully this will make someone's life a bit easier, at least in the short term. 
 
Update: some people have had trouble opening this solution.  The solution was created in VS 2005 Team Edition for Software Developers. It contains the main project as well as a test project and some solution items.  If you experience trouble, simply open Visual Studio 2005 and open the single project (PS.WCF.Configuration) which is a simple c# project. 
 
Update: I've updated this sample to work with the Feb CTP.  I don't like this solution as much b/c it is not as clean, but the BaseAddresses collection is now readonly so I can't update it in the ApplyConfiguration method.  Instead, I am creating the collection in my constructor, which I really don't like, but it does work.  I'm hoping this gets easier in the later releases, not harder.

Tags:

Windows Communication Foundation

Pluralsight Developer Training

View my developer training videos on Pluralsight.

Pluralsight .NET Training

Tag cloud