This tutorial covers following :
- Creating a Sequential workflow using code-separation authoring mode
- Use Visual Studio 2005 and Windows Workflow Foundation extensions for VS
- Demonstrates IfElse activity and rule based condition authoring
To start follow the first three steps as in following post.
- In the next step drag and drop the IfElse activity on to the empty workflow file. The ifElseActivity with two ifElseBranchActivity will be added to your workflow
- Also add the code activities for each of the IfElseBranch activities which will get executed whenever that branch evaluated to true. This can be done by dropping the Code Activity on each of the branch and setting the value of ExecuteCode parameter in the properties window. e.g. codeCondition1 and codeCondition2. Both the methods will be generated in code file. Modify the code as below :
private void codeCondition1(object sender, EventArgs e)
{
Console.WriteLine("i equals zero is true");
}
private void codeCondition2(object sender, EventArgs e)
{
Console.WriteLine("i equals one is true");
}
- Next add an instance member to your workflow class e.g. int i. The workflow class now will look like below :
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace RuleCondition
{
public partial class Workflow1 : SequentialWorkflowActivity
{
int i=1;
private void codeCondition1(object sender, EventArgs e)
{
Console.WriteLine("i equals zero is true");
}
private void codeCondition2(object sender, EventArgs e)
{
Console.WriteLine("i equals one is true");
}
}
}
- Switch to design mode of workflow.In the properties windows of first ifElseBranch activity set the following parameters as below :
- Condition -> Declarative Rule Condition
- ConditionName -> iEqualsZero
- Expression( which contains the actual condition to be evaluated) -> this.i==0 ; which means if i equals zero execute this branch.
- As soon as these parameters are set a new file will be generated and added to project called Workflow1.rules where the above set condition is actually defined.
- Condition -> Declarative Rule Condition
- Similarly set the parameters to second Branch also as below:
- Condition -> Declarative Rule Condition
- ConditionName -> iEqualsOne
- Expression -> this.i==1
- Condition -> Declarative Rule Condition
- Add a new code file to the project which will the entry point for this console application and will also host the created workflow :
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace RuleCondition
{
class Program
{
static AutoResetEvent waitHandle = new AutoResetEvent(false);
static void Main(string[] args)
{
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {OnCompleted();};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(RuleCondition.Workflow1));
instance.Start();
waitHandle.WaitOne();
}
}
static private void OnCompleted()
{
waitHandle.Set();
Console.WriteLine("wf completed");
Console.Read();
}
}
}
- Build and execute the project. As we have set the value of i=1 , the following output will be displayed :
i equals one is true
wf completed
- Open the Workflow1.rules file. Well don't get scared, although its big for creating such a simple condition but its quite simple to understand. The main elements you need to look for are : RuleExpressionCondition, CodeBinaryOperatorExpression, CodeFieldReferenceExpression, CodePrimitiveExpression.Value. The file will have following code :
<RuleDefinitions xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
<RuleDefinitions.Conditions>
<RuleExpressionCondition Name="iEqualsZero">
<RuleExpressionCondition.Expression>
<ns0:CodeBinaryOperatorExpression Operator="ValueEquality" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<ns0:CodeBinaryOperatorExpression.Left>
<ns0:CodeFieldReferenceExpression FieldName="i">
<ns0:CodeFieldReferenceExpression.TargetObject>
<ns0:CodeThisReferenceExpression />
</ns0:CodeFieldReferenceExpression.TargetObject>
</ns0:CodeFieldReferenceExpression>
</ns0:CodeBinaryOperatorExpression.Left>
<ns0:CodeBinaryOperatorExpression.Right>
<ns0:CodePrimitiveExpression>
<ns0:CodePrimitiveExpression.Value>
<ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">0</ns1:Int32>
</ns0:CodePrimitiveExpression.Value>
</ns0:CodePrimitiveExpression>
</ns0:CodeBinaryOperatorExpression.Right>
</ns0:CodeBinaryOperatorExpression>
</RuleExpressionCondition.Expression>
</RuleExpressionCondition>
<RuleExpressionCondition Name="iEqualsOne">
<RuleExpressionCondition.Expression>
<ns0:CodeBinaryOperatorExpression Operator="ValueEquality" xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<ns0:CodeBinaryOperatorExpression.Left>
<ns0:CodeFieldReferenceExpression FieldName="i">
<ns0:CodeFieldReferenceExpression.TargetObject>
<ns0:CodeThisReferenceExpression />
</ns0:CodeFieldReferenceExpression.TargetObject>
</ns0:CodeFieldReferenceExpression>
</ns0:CodeBinaryOperatorExpression.Left>
<ns0:CodeBinaryOperatorExpression.Right>
<ns0:CodePrimitiveExpression>
<ns0:CodePrimitiveExpression.Value>
<ns1:Int32 xmlns:ns1="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">1</ns1:Int32>
</ns0:CodePrimitiveExpression.Value>
</ns0:CodePrimitiveExpression>
</ns0:CodeBinaryOperatorExpression.Right>
</ns0:CodeBinaryOperatorExpression>
</RuleExpressionCondition.Expression>
</RuleExpressionCondition>
</RuleDefinitions.Conditions>
</RuleDefinitions>
~bye & take care~
Other Posts
- Developing Code Condition workflow using IfElse activity
- Defining Conditions based WorkFlow
- How to develop a simple sequential workflow
- Components required to develop WorkFlow (WF) enabled applications
- Windows WorkFlow Foundation (WF) : An Overview
- Increasing Productivity/Discipline using Project Templates
- Microsoft Office System 2007 : The Composition
Thanks, this has been very helpful
ReplyDelete