Partilhar via


Constraint Types

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

This sample shows two different ways to apply constraints to a workflow, one is from inside the activity (build) and one is from outside of it (policy). In this scenario, an activity author (from a 3rth-party software company) wants to validate the relationship between two arguments. In this case, the cost should be smaller than or equal to the price. This is a general validation build constraint.

Then a shop owner wants to add some validation to this generic activity. In his case, he wants the majority of its products to be $9.99 or less. So, he uses a policy constraint that is on top of the CreateProduct activity.

In the sample:

The activity author (build) must:

  • Create a constraint (PriceGreaterThanCost). This is where all the validation logic resides.

  • Override OnGetConstraints and add the constraint (PriceGreaterThanCost) to the constraints IList.

The workflow author (policy) must:

  • Create a workflow with an instance of the activity to validate (CreateProduct).

  • Create a constraint (MaxPrice).

  • Create a ValidatorSettings instance (validatorSettings) and add the constraint (MaxPrice) to the collection AdditionalConstraints. Here the workflow author can add policy constraints to any activity, such as a sequence or parallel.

  • Call Validate, which returns a ValidationResults collection of ConstraintViolation objects.

  • (Optional) Print the ConstraintViolation objects.

To set up, build, and run the sample

  1. Open the ConstraintTypes.sln sample solution in Visual Studio 2010.

  2. Build and run the solution.

Dd759025.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\Validation\ConstraintLibrary