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


Windows Workflow Foundation

&amp;quot;Beta 2&amp;quot; of WinFX (WCF, WPF and Workflow Foundation) has arrived

by Matt Milner 18. January 2006 09:47
The Jan CTP for these products is now up on the Microsoft website and they even have go live licenses available!  I know a lot of people are eager to get a look at the updates, especially with workflow, so let the downloading begin!


Windows Workflow Foundation

MSDN Magazine article on VS.Net Templates

by Matt Milner 4. January 2006 15:26
My article on VS.Net templates is finally up on the MSDN Magazine web site [1].  I had a lot of fun diving into this feature and look forward to being able to use them in creating classroom materials and templates for my customers. 


General Musings

Understanding the FactHandle in a FactRetriever

by Matt Milner 4. January 2006 15:23
One of the most confusing things, other than that pesky document type problem, that developers I work with run into with the business rules engine in BizTalk is understanding the role of the fact handle when implementing a fact retriever. 
A FactHandler, for those that are not familiar, is a class that one implements in order to supply long term facts to the Business Rule Engine in BizTalk Server.  Common items returned include database connections or tables, or xml content from a database or file.  The business rule engine calls the fact handler on each invocation of any policy it is configured on to get these facts.  It is the job of the fact retriever to make sure that it provides up to date information as appropriate and caches data or variables where necessary. 
It would seem, from the name of the variable, "factHandle", and the way it is passed to the single function in the IFactRetriever interface, that this object is actually the fact(s) being returned by the fact handler.  In reality, however, this object is actually the item that helps the fact retriever be smart about caching and retrieving data.  The act of getting facts into the rule engine occurs by Asserting items into the engine within the fact retriever method. 
The fact handle is the object that your fact retriever creates the first time it is called.  This object is then passed back in to your fact retriever each time the retriever is called so that it can use this information to determine if it needs to refetch data or just allow the business rule engine to use the data it returned last time.  A simple, yet effective example, is to return a DateTime struct on the first request and then, on subsequent calls, check it against a configured timeout to determine if you should fetch data again. 
The code below shows both the UpdateFacts method as well as a helper method to determine if the facts are out of date. 
public object UpdateFacts(RuleSetInfo ruleSetInfo, RuleEngine engine, object factsHandleIn)
  object factsHandleOut = null;
  if(factsHandleIn == null || FactsAreOutOfDate(factsHandleIn))
    //Assert facts into the engine at this point using the "engine" parameter
    //If this is the first time here, or we are out of date
    //then update the facts handle to the current time
    factsHandleOut = DateTime.Now;
  factsHandleOut = factsHandleIn;
  return factsHandleOut;
private bool FactsAreOutOfDate(object factsHandle)
  DateTime lastUpdateTime;
  if(factsHandle == null)
     return true;
  if(factsHandle is DateTime)
    lastUpdateTime = (DateTime)factsHandle;
    return (TimeSpan.Compare(DateTime.Now.Subtract(lastUpdateTime),
                    TimeSpan.FromDays(1)) > 0);
    return true;
In the first method, we test for the fact handle to be null or to be out of date.  We do a simple check to see if the facts are more than a day old.  If they are, then we return true so that we know to reassert the facts. Back in the UpdateFacts method, we either return the original facts handle if we don't want to make any changes, or we return the current time if we are asserting new facts and want to reset our "timer". 
This is obviously a simple example and might be extended by using a class or struct to wrap multiple DateTime structs representing different facts. That is, we might not want to treat all of the facts with the same policy, so we can have timeouts for different facts.  A database connection we might not refresh, but a datatable we might refresh every few hours. 
Hopefully, this information will be helpful to people trying to work with the business rules engine in BizTalk. 


BizTalk Server

Back to blogging

by Matt Milner 4. January 2006 15:23
Well, I apologize for being so lax in my posting.  I finished off the year with a bang and haven't had time to post.  I've been traveling to exotic places like Bangalore, India. I had a great time with some ISVs and the folks from Microsoft talking about Workflow and Windows Communication.  This was my first trip to India and it was a fun one.  I did have some huge trouble with jet lag; I didn't sleep until the fourth night there and it took me a full week to get a decent night sleep.  But, all that aside, I had a great time with some fabulous food, great hospitality, and lots of good conversation about technology! 
Starting off the new year, I am going to try and get some posts out before I head out to London and Paris in a couple of weeks.  I've got a post that I'll put up tonight along with this one related to the business rules engine in BizTalk.  I'll also be posting some items on workflow and some sample activities that will hopefully prove helpful in showing off features. 


General Musings

The difference a checkbox can make

by Matt Milner 3. October 2005 21:13

I was doing some work recently on upgrading BizTalk 2004 projects to 2006 and had been using the same web services that I had originally published using the BTS 2004 wizard.  I had some changes to make and so I fired up the BTS 2006 web services publishing wizard, not expecting to find much new.  However, one checkbox made the experience worth it.  It said: Merge selected ports into a single web service.  Glorious!  I can't tell you how many times I have had to either create a single port definition, shared by multiple orchestrations, so that I could publish one instance of the port, or hand editing the published web services to get a single service. 

In addition to this one simple change, there is another change that I was glad to see with this wizard.  You can now use the SoapParameterStyle.Bare option to have a little more control over the WSDL contract for your service.  This is especially useful if you are trying to implement an existing interface that requires this style of parameter passing.  You access this with the advanced button on the additional settings page when you are setting things like web service namespace and SOAP header support. 

Additions like these will continue to make BizTalk the tool of choice for implementing SOA.

Enjoy the update wizard, I know if will. 


Week of Workflow Webcasts

by Matt Milner 15. September 2005 03:05

If you are interested in finding out more about Windows Workflow Foundation (WWF) then you should check out the upcoming week of webcasts.  See Paul's Blog for more details. 


Windows Workflow Foundation

Windows Workflow Foundation- What about BizTalk?

by Matt Milner 14. September 2005 12:42

So, with the big announcement about the upcoming release of Windows Workflow Foundation (WWF - gotta love that acronym; I was surprised not to see Hulk Hogan here presenting), many BizTalk developers have already started asking what this means for BizTalk Server. 

The simple answer is that BizTalk 2008 (or whatever the official name becomes, but not 2006) will host the workflow runtime for its orchestration capabilities and will extend workflow with all of the great things you've come to expect from BizTalk.  BizTalk Server will be your enterprise solution for scalable, reliable message based integration, business process management and SOA applications and services.  BizTalk will be providing MANY important features on top of the basic workflow foundation that you will want for your EAI, B2B, and SOA apps, like pipelines, adapters, management, infrastructure, security, etc.  So, before you run off and say, "With WCF ("Indigo") and WWF, I can build my own BizTalk"; think twice, or maybe five times.  The BizTalk team has years of experience and customer data points for building the manageability, scalability and reliability that an application of this type demands and they will continue to deliver a product that goes far beyond the capabilities of the WWF. 

However, the great news is, skill sets in WWF, which will likely reach more developers, will be greatly applicable to BizTalk Server 2008 development.  And, because the workflow foundation is extensible, it is likely that BizTalk developers will get their biggest wishes answered in two ways.  1) BizTalk Server 2008 will almost definitely include VS.Net debugging for orchestrations as this is already built into WWF. 2) BizTalk 2008 will easily have the ability to allow developers to create and use custom actions, and we'll have to watch for the product team to include. 

All that being said, and my passion for BizTalk being obvious, WWF is hugely powerful framework on its own and will allow developers to create amazing applications while improving their productivity.  The activity model will also help in the ever present goal of making reusable components.  So, I'm very excited about WWF, and glad we can now talk about it publicly.  Look for more information here on my blog and the Pluralsight web site for upcoming announcements related to WWF, WCF and BizTalk. 



BizTalk Server | Windows Workflow Foundation

China, Iowa, and in between

by Matt Milner 1. September 2005 06:05

So my lame excuse for not blogging for months is that I have been traveling all over the world.  That's not entirely true, and I've certainly had time in the office, I've just been finding a lot to do.  So, here is the run down of what I have been doing, and more importantly, what I plan on doing and blogging about in the next couple of weeks.

So first off, I went to Shanghai, China to deliver the Applied Web Services class for a group of Microsoft folks there.  It was a great bunch of people with varying reasons for being interested in web services.  The trip was fabulous and it was a great experience for me, having never been to China.  Sorry, I don't have any pictures yet, as that is another thing I haven't gotten around to: taking my disposable camera in for development. 

So that was my excuse for June, that and Tech Ed, but then I had to get ready for RAGBRAI, the great bicycle ride across Iowa. My in-laws have been doing the ride for years, and having grown up in Iowa, I had always wanted to try it so this was the year.  485 miles was tough on some days, but I made it through and even made it up the big hills without having to get off and walk.  If I do it again next year, I'll be sure to train a little harder for those hills.  We had a lot of fun and took my 1 year old son along.  The first night we camped with him in a tent and had 70 MPH winds and downpour of rain.  He slept through some of the loudest thunder claps, and only woke up and got upset in the morning when he realized he was soaking wet.  He's a trooper!  

So what's coming up: 

PDC - I'll be there, just doing the attendee thing this time and excited to learn about Office 12 and workflow.  I'll probably check out an Indigo talk or two as well.  :) 

HDC - The heartland developer conference is Oct 12 - 14 and will be a lot of fun.  If you are in the central region you should check it out.  I'm presenting on XML in SQL Server and I'll be looking at the server side as well as programming the client side. 

I've got an article coming out in MSDN magazine on Visual Studio Templates and Starter Kits.  When I have a definite date, I'll pass it along.  For us trainer types, these are very cool as they allow us to reuse initial solutions over and over.  The article, however, is geared toward business/enterprise developers who want to leverage templates for projects and items to have some consistency in their organization.  These are very cool stuff and hopefully the article will help you get started with them. 

Now, for blogging, I have lots to write about, including BizTalk pipeline configuration and dynamic message part creation in orchestrations and pipelines.  I've also done some fiddling with BizTalk web services for a project I'm working on and hope to share some insights on how to tweak those generated services when you need/want to.  In addition, I'll be posting on some of the new features in BizTalk 2006 that I really like around web services, pipelines and orchestrations.  



ASP.Net SQL Cache Dependencies with SQL 2005

by Matt Milner 15. June 2005 04:33
I was testing out some ASP.Net features and got SQL cache dependencies working just fine with SQL 2000.  However, I wanted to  see the new features of SQL 2005 using query notification to get immediate cache invalidation without polling the database.  I couldn't find any documentation, so I did a little spelunking.  It turns out that instead of configuring a dependency and referencing it in the properties for the SQL data source like you do for SQL 7.0 and 2000, you use "CommandNotification" as the SQL cache dependency property.  This tells ASP.Net that you are using SQL 2005 and it then correctly uses query notification instead of trying to use the polling mechanism that is required for earlier versions of SQL. 
The great thing about this solution is that it does not require you to run any setup scripts or create triggers and stored procedures in your database.  It does however require that you enable Service Broker on your SQL database which is not enabled by default for security reasons. 
I hope this helps some other people trying out the betas. 


SQL Server 2005

Pluralsight Developer Training

View my developer training videos on Pluralsight.

Pluralsight Hardcore Training