Windows Workflow allow us to perform custom validator input that is executed at design and compile time. The error output is similar as Biztalk Orchestration shape errors.

Adding a validator to a custom activity involves two steps:

  1. Create a class that derives from ActivityValidator class and override Validate method;
  2. Add a ActivityValidator attribute with the validator type to the custom activity;

As an example, i'm going to show a validator creation that verifies if a specific property is set.

I have already created a custom activity that is called Hello and have a property that receives the user name. To validate if the activity proprety is set, i'm going to create a validator. 

ActivityValidator class creation:

As said early, to create a validator i have to create a class that derives from ActivityValidator. To perform the validation i have to override the validate method and set the validation code.

using System;

using
System.Text.RegularExpressions;

using
System.Workflow.ComponentModel.Compiler;

internal sealed class ExampleValidator : ActivityValidator {

    public override ValidationErrorCollection Validate (ValidationManager manager, object obj)
    {

        if (manager == null throw new ArgumentNullException("Invalid manager.");)

        if (obj == null throw new ArgumentNullException("Invalid activity.");)

        HelloActivity activity = obj as HelloActivity

        if (activity == null) throw new InvalidOperationException("Activity should be a HelloActivity.");

        ValidationErrorCollection errors = base.Validate(manager, obj);

        // Validate UserName property setting validationErrorCollection with information
        // about the property that causes the error.

        if (string.IsNullOrEmpty(activity.UserName)) {
           
errors.Add(ValidationError.GetNotSetValidationError("Username"));
        } 

        return errors;
    }
}

 

Custom activity and validator association

To associate the validator with the custom activity it's used the activityvalidator attribute.

[ActivityValidator(typeof(ExampleValidator))]

public sealed class HelloActivity : System.Workflow.ComponentModel.Activity {..

 

In the end the custom activity validator error will be show like this:

WFValidator 

LEAVE A REPLY

Please enter your comment!
Please enter your name here