Partager via


Tutorial: Use Rules and Conditions in WF

Activity conditions are used by certain types of activities in the Windows Workflow Foundation (WF) base activity library, such as the WhileActivity activity and IfElseActivity activity. These types of activities contain child activities that may or may not be executed based on their associated activity condition. Activity conditions evaluate to either true or false, and the results of these evaluations are used by these activities to determine whether to execute the desired child activities. In WF, there are two types of activity conditions: imperative and declarative. Imperative conditions are code based and use the CodeCondition type and declarative conditions use RuleConditionReference. Both of these types derive from the abstract base class ActivityCondition and you can create your own custom activity condition types by deriving from this class. In this tutorial, the usage of both imperative and declarative conditions in conjunction with the WhileActivity activity and the IfElseActivity activity are covered.

Rules are declarative expressions that are contained in a RuleSet. This RuleSet is referenced by a PolicyActivity that is hosted by a workflow. These rules are in the format of if/then/else, and within these clauses, a Rule can access members of the workflow that contains the PolicyActivity. In this tutorial, two PolicyActivity activities are used. The first example shows basic rules functionality and the second shows examples of rules chaining and rule dependencies and re-evaluation.

The application that you build in this tutorial is a number guessing game. A random number is generated, and then you attempt to guess the number. If you are too low or too high you are prompted until you correctly guess the number. At that point you are ranked based on the number of guesses it took, and then the computer attempts to guess the number. When successful, it reports on how many guesses it took.

To achieve this, you create a Sequential Workflow Console Application using the appropriate project template. The workflow uses a combination of activities to implement the game portion of the application. Specifically, you use activity conditions and rules by using a WhileActivity activity, an IfElseActivity activity, and PolicyActivity activities.

In This Section

  • Exercise 1: Create the Workflow Application
    Creates the project for the Number Guessing application that is used in this tutorial. Then the host application is configured and the workflow implements the basic version of the application.

Reference

CodeCondition

RuleConditionReference

ActivityCondition

WhileActivity

IfElseActivity

RuleSet

PolicyActivity

Workflow Changes to Rule Conditions

Using Conditions in Workflows

How to: Use Conditions Through Code

Rules Evaluation in RuleSets

Rules and Conditions

Using the PolicyActivity Activity

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04