Activity Validation Logic Overview 

The validator component of an activity houses all design-time and run-time validation logic. This ensures that the activity's properties that are defined in the activity's metadata are appropriately configured.

The activity framework provides extensive default validation capabilities, so in many cases you do not have to write a custom validator for your activity. If you do write a custom validator class, your custom class must inherit from Validator.

NoteNote

By default, activity instance properties are not validated even when you set the ValidationOptionAttribute to ValidationOption.Required. If you want instance properties in your custom activities to be validated, you must create your own custom validator to do this.

Validation occurs automatically during the compilation of a workflow and also whenever the Validate method of a validator component for an activity is called with the corresponding activity metadata object as a parameter. This enables a workflow authoring tool to use a "correct-by-construction" paradigm if it chooses.

Additionally, validation occurs on a workflow instance at run time when a workflow change is performed. This validation might differ from the validation performed at compile time. This functionality ensures the safety of a run-time operation such as the addition or replacement of an activity in the activity tree of a running workflow instance.

The following example shows how to create a custom activity validator by deriving from the ActivityValidator class and overridding the Validate method to add custom validation code.

Public Class CustomActivityValidator
    Inherits ActivityValidator
    Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
        Dim activity As Activity1 = CType(obj, Activity1)
        If activity.Parent IsNot Nothing Or activity.Activities.Count <> 0 Then
            Dim errors As ValidationErrorCollection = New ValidationErrorCollection()
            errors.AddRange(MyBase.Validate(manager, obj))
            Return errors
        End If
        Return New ValidationErrorCollection()
    End Function
End Class

public class CustomActivityValidator : ActivityValidator
{
    public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
    {
        Activity1 activity = obj as Activity1;
        if (activity.Parent != null || activity.Activities.Count != 0)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();
            errors.AddRange(base.Validate(manager, obj));
            return errors;
        }
    return new ValidationErrorCollection();
    }
}

To use your custom validator with your activity, you must decorate your activity with the ActivityValidatorAttribute as shown in the following example.

<ActivityValidator(GetType(CustomActivityValidator))> _
Public Class Activity1
    Inherits SequenceActivity

    Public Shared MyPropertyProperty As DependencyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", GetType(String), GetType(Activity1))

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    Public Property MyProperty() As String
        Get
            Return (CType((MyBase.GetValue(Activity1.MyPropertyProperty)), String))
        End Get
        Set(ByVal Value As String)
            MyBase.SetValue(Activity1.MyPropertyProperty, value)
        End Set
    End Property
End Class

[ActivityValidator(typeof(CustomActivityValidator))]
public partial class Activity1: SequenceActivity
{
    public Activity1()
    {
        InitializeComponent();
    }

    public static DependencyProperty MyPropertyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", typeof(string), typeof(Activity1));

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string MyProperty
    {
        get
        {
            return ((string)(base.GetValue(Activity1.MyPropertyProperty)));
        }
        set
        {
            base.SetValue(Activity1.MyPropertyProperty, value);
        }
    }
}

See Also

Footer image
Send comments about this topic to Microsoft.
Show: