Policy Activity in .NET Framework 4

This topic applies to Windows Workflow Foundation 4 (WF4).

The Policy4 activity allows Windows Workflow Foundation in .NET Framework version 3.5 (WF 3.5) RuleSet objects to be used in Windows Workflow Foundation in .NET Framework version 4 (WF 4) directly by using the rules engine that is shipped in WF 3.5. By using this activity, you can create and execute a WF 3.5 RuleSet. For more information about WF 3.5 Rules Engine included as part of Windows Workflow Foundation, please read Introduction to the Windows Workflow Foundation Rules Engine. For more information about migrating rules to WF in .NET Framework 4, please read Migration Guidance.

Dd797584.Important(en-us,VS.100).gif Note:
The samples may already be installed on your computer. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WF\Scenario\ActivityLibrary\Rules-Policy4

Projects in this Sample

Project Name Description Main Files

Policy4

Contains the Policy4 activity and its WF designer.

Policy4.cs: Policy4 activity definition.

PolicyDesigner.xaml: Custom designer for Policy4 activity. It uses the rules editor (RuleSetDialog Class) from WF rules engine.

ImperativeCodeClientSample

Sample client application that configures and runs a workflow using a Policy4 application using imperative C# code (no WF Designer used).

ApplyDiscount.rules: File with WF rule definitions.

Order.cs: Type that represents a customer order. Rules are applied to objects of this type.

Program.cs: Configures and runs a workflow that has a Policy4 activity to apply rules defined in ApplyDiscount.rules to instances of Order objects.

App.config: Configuration file with the path of the rules file.

DesignerClientSample

Sample client application that configures and runs a workflow using a Policy4 application in the WF Designer.

Sequence1.xaml: Sequential workflow that uses a Policy4 activity to perform rule evaluations.

Program.cs: Runs an instance of the workflow defined in Sequence1.xaml.

The Policy4 Activity

The Policy4 activity is a class that derives from NativeActivity that allows workflows to execute WF RuleSets. The following code example is a simplified definition of the public OM of the activity.

public class Policy4Activity<TResult>: NativeActivity<TResult>
{
    public RuleSet RuleSet

    [IsRequired]
    public InArgument Input

    public OutArgument<ValidationErrorCollection> ValidationErrors
}

Property Description

RuleSet

The WF RuleSet Class for .NET Framework 3.5 to be evaluated when the activity is executed.

TargetObject

The object against which the Rules in the RuleSet Class are evaluated.

ValidationError

The list of validation errors returned by the WF Rule Engine for .NET Framework 3.5 when validating the RuleSet Class against the target object before execution.

Policy4 Activity Designer

The Policy4 designer adds the capability to configure a Policy4 activity without the need to write code. After building the solution, it can be found in the toolbox in the section Microsoft.Samples.Activities.Rules.

The WF Designer allows you to configure a target object and a RuleSet. When the Edit RuleSet button is clicked, the WF RuleSetDialog Class for .NET Framework 3.5 is displayed. This dialog is the re-hosted .NET Framework 3.5 Rules Editor. Use the editor to create and edit the rules that the Policy4 activity executes.

Using this Sample

No special set up is required to run this sample. Just open the solution in Visual Studio, and press F5 to run the application.

This sample contains two client applications: ImperativeCodeClientSample and DesignerClientSample. The ImperativeCodeClientSample client shows how to configure and run the Policy40 activity using C# imperative code. The DesignerClientSample shows how to configure and run the Policy4 activity using the designer.

To run the ImperativeCodeClientSample client application

  1. Using Visual Studio 2010, open the Policy4Sample.sln solution file.

  2. In Solution Explorer, right-click the ImperativeCodeClientSample project and then select Set as startup project.

  3. To run the project, press CTRL+F5.

To run the ImperativeCodeClientSample client application

  1. Using Visual Studio 2010, open the Policy4Sample.sln solution file.

  2. In Solution Explorer, right-click the DesignerClientSample project.

    • Select Set as startup project.
  3. To compile the project, press CTRL+SHIFT+B.

  4. To run the project, press CTRL+F5.