WhileActivity and ActivityExecutionContext in WF

WhileActivity is a special activity in WF when ActivityExecutionContext (AEC) is concerned, because it creates a new AEC for each iteration.

Why? You can know the reason from common programming languages. For example, the below C# while statement adds/removes local variables from stack for each iteration:
while (conditionIsMet)
string output = DateTime.Now.ToString();
int count = 0;

WF WhileActivity has similar logic for each iteration: to create a new AEC based on the template activity (e.g. the SequenceActivity inside the WhileActivity).

Will the new AEC be destroyed at the end of each iteration? You may think "Of course! This is an obvious question". But the answer is "yes or no".

The answer is yes for normal cases without the need for compensation. There will be only one new AEC in memory for the iterations.

But the answer is no when compensation is needed. If a CompensatableActivity is defined inside the WhileActivity and when an exception occurrs in one iteration, all the previous iterations will be compensated. That means all previous AEC's can not be destroyed after each iteration. They have to be in memory for compensation purpose.

According to Krishnan:"The runtime will clean up execution contexts after each WhileActivity iteration. But only if you don't have an ICompensatableActivity inside (if you do, the EC's will stay in memory until the next persistence point.)"


pawan ashish said...

i have seen a lot of post on while activity with ActivityExecutionContext but can any one tell me how to use it.
i have a while loop inside that i have a ifelse activity and inside that i have sendmail . sendmail is working fine outside the while loop but i put it inside the while loop it doesn't works. can any one help me

Jun Meng said...

Hi Pawan,

The ActivityExecutionContext is inside WF engine. It has nothing to do with SendMail working or not.

Maybe you can check if your virus software blocks your application to send email or not. To avoid spam virus, virus software normally blocks SMTP port.

Anonymous said...
This comment has been removed by a blog administrator.