1. Problem
You need to repeat a series of steps in an orchestration until a certain condition is met.
2. Solution
You can use the Loop shape in a
BizTalk orchestration, in a manner similar to using a loop in any
programming language, such as this looping logic:
int a = 0;
while(a < 3)
{
System.Console.WriteLine(a);
a = a + 1;
}
As an example, the
following steps show how to implement a loop that terminates after a
counter variable has been incremented to a certain value. The
orchestration will loop three times, logging its progress to the Windows
Event Viewer.
NOTE
The example
demonstrates a complete orchestration that could be called from another
orchestration using the Call or Start Orchestration shape. To make this a
stand-alone orchestration, simply add a Receive shape as the first step
and bind it to a port.
In an empty orchestration, create a new variable called intCount. Make it of type Int32. This will represent the loop counter.
Drop an Expression shape on the design surface. Rename this shape to Set_Count. Then double-click the shape, and type in the following code:
//initialize counter
intCount = 0;
Drop a Loop shape below the Set_Count shape. Double-click the Loop shape, and enter the following code (note there is no semicolon):
//loop while count is less than 3
intCount < 3
Drop another Expression shape inside the Loop shape, and rename it Increase_Count. Enter the following code:
//increase counter
intCount = intCount + 1;
//log to the event viewer
System.Diagnostics.EventLog.WriteEntry("Count",
System.Convert.ToString(intCount));
The orchestration is shown in Figure 1.
3. How It Works
The Loop shape has
many applications, from looping through XML documents to supplementing
the Listen shape, exception handling routines, and so on. Orchestrations
can mimic the behavior of long-running Windows services with the proper
placement of a Loop shape. For example, if you need to poll for data on
a timed interval, you could set up an orchestration to do this. An
initial Receive shape would instantiate the orchestration and
immediately enter a loop. Once in the loop, it would never exit, looping
every [x] number of minutes to
reexecute a series of steps. The orchestration would be long-running and
would never end until terminated manually. An example of this type of
orchestration is shown in Figure 2.