Imperative Code-Based Validation

Imperative code-based validation provides a simple way for an activity to provide validation about itself, and is available for activities that derive from CodeActivity, AsyncCodeActivity, and NativeActivity. Validation code that determines any validation errors or warnings is added to the activity.

Using Code-Based Validation

Code-based validation is supported by activities that derive from CodeActivity, AsyncCodeActivity, and NativeActivity. Validation code can be placed in the CacheMetadata override, and validation errors or warnings can be added to the metadata argument. In the following example, if the Cost is greater than the Price, a validation error is added to the metadata.

Note

Note that Cost and Price are not arguments to the activity, but are properties that are set at design time. That is why their values can be validated in the CacheMetadata override. The value of the data flowing through an argument cannot be validated at design time because the data does not flow until run time, but activity arguments can be validated to ensure that they are bound by using the RequiredArgument attribute and overload groups. This example code sees the RequiredArgument attribute for the Description argument, and if it is not bound then a validation error is generated. Required arguments are covered in Required Arguments and Overload Groups.

public sealed class CreateProduct : CodeActivity  
{  
    public double Price { get; set; }  
    public double Cost { get; set; }  
  
    // [RequiredArgument] attribute will generate a validation error
    // if the Description argument is not set.  
    [RequiredArgument]  
    public InArgument<string> Description { get; set; }  
  
    protected override void CacheMetadata(CodeActivityMetadata metadata)  
    {  
        base.CacheMetadata(metadata);  
        // Determine when the activity has been configured in an invalid way.  
        if (this.Cost > this.Price)  
        {  
            // Add a validation error with a custom message.  
            metadata.AddValidationError("The Cost must be less than or equal to the Price.");  
        }  
    }  
  
    protected override void Execute(CodeActivityContext context)  
    {  
        // Not needed for the sample.  
    }  
}  

By default, a validation error is added to the metadata when AddValidationError is called. To add a validation warning, use the AddValidationError overload that takes a ValidationError, and specify that the ValidationError represents a warning by setting the IsWarning property.

Validation occurs when a workflow is modified in the workflow designer and any validation errors or warnings are displayed in the workflow designer. Validation also occurs at run time when a workflow is invoked and if any validation errors occur, an InvalidWorkflowException is thrown by the default validation logic. For more information about invoking validation and accessing any validation warnings or errors, see Invoking Activity Validation.

Any exceptions that are thrown from CacheMetadata are not treated as validation errors. These exceptions will escape from the call to Validate and must be handled by the caller.

Code-based validation is useful for validating the activity that contains the code, but it does not have visibility into the other activities in the workflow. Declarative constraints validation provides the ability to validate the relationships between an activity and other activities in the workflow, and is covered in the Declarative Constraints topic.