Workflow Foundation Rule Engine – Policy Activity

Posted: March 29, 2009 in .NET, WF

In my last post I had written about the Rule Engine that comes embedded with Windows Workflow Foundation and in this post I will show how we can use this with the PolicyActivity.

The System.Workflow.Activities.PolicyActivity class is a specialized workflow activity class that encapsulates a RuleSet.The PolicyActivity.RuleSetReference property maintains reference to a RuleSet stored in a .rules file.During execution time the PolicyActivity retrieves the rules from the .rules file and execute the rules.

The demo that I have prepared implements a very simple rule to calculate discount, based on order value,if order value is > 100 and < 200 then discount is 10% and if > 200 then 20%.

  • Step1: Create a Sequential Workflow Library and add a Policy Activity.


  • Step2: Define the orderValue and discount properties in DiscountManager.cs

           public double orderValue { get; set; }
           public double discount { get; set; }

  • Step3: Add a PolicyActivity using the VS 2008 Toolbox as shown below:


  • Step4:Click on the RuleSetReference property to open the Rule Editor and define the rules as shown below.Here the conditions and actions will be
    • this.orderValue >= 100 && this.orderValue <=200, then
      • = 0.1 (10%)
    • this.orderValue > 200 then
      • = 0.2 (20%)




  • Step5:Now I have added a method to be executed in the Closed event of policy activity as shown below to print the calculated values:

          void SetDiscount_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)
                 Console.WriteLine(“Discount on Ordervalue {0} is {1}”, orderValue, discount);

  • Step6: Add another Console program in the solution.Add Reference to System.Workflow.Runtime,System.Workflow.ComponentModel and System.Workflow.Activities.Also add reference to the WF library we created in steps above.
  • Step7: Add the following lines of code to start and execute the workflow in Main method.using System;
    using System.Collections.Generic;
    using System.Workflow.Runtime;
    using DiscountPolicy;
    using System.Threading;
    namespace DiscountCalculator
        class Program
            static AutoResetEvent waitHandle = new AutoResetEvent(false);
            static void Main(string[] args)
                //Instantiate the Workflow Runtime
                using (WorkflowRuntime wr = new WorkflowRuntime())
    //Start the runtime
    //Add Completed Event
                    wr.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wr_WorkflowCompleted);
    //Pass parameter as dictionary
                    Dictionary<string, object> param = new Dictionary<string, object>();
                    param.Add(“orderValue”, 120.5);
    //Create Workflow instance
                    WorkflowInstance wi = wr.CreateWorkflow(typeof(DiscountManager),param);
    //Start workflow
    //Wait for completion
    //Stop Runtime


  • Step8: Now compile the solution and run the console program.The output will be

                      Discount on Ordervalue 120.5 is 0.1

So our rule engine is demo is complete.In the next post we will move to topics like priority and chaining etc.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.