简体   繁体   中英

How to check property value in a WCF activity?

I am really new to windows workflow and I need to create an activity. I did that:

class CustomActivity : Activity { }

This activity has custom a property and I did that:

class CustomActivity : Activity 
{
    /// <summary>
    /// Creation of the Value Property.
    /// </summary>
    [Description("The value of the property to set")]
    [Category("Configuration")]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Value { get; set; }
}

Now I would like to check what the user set in this property when he uses the designer. For example, if he compiles the workflow, is there any callback when the workflow is compiled, so I could generate compilation error ? Or any integrity check callback ?

Thanks for you help.

I found the solution by reading WF documentation. It is quite easy:

Create a validator object:

class CustomActivityValidator : ActivityValidator
{
    public override ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj)
    {
        if (null == manager)
        {
            throw new ArgumentNullException("manager");
        }

        if (null == obj)
        {
            throw new ArgumentNullException("obj");
        }

        CustomActivity activity = obj as CustomActivity;
        if (null == activity)
        {
            throw new ArgumentException("This validator can only be used by the CustomActivity", "obj");
        }

        ValidationErrorCollection errors = base.ValidateProperties(manager, obj);
        if (null != activity.Parent)
        {
            // Now actually validate the activity...
            if (activity.Value != "foobar")
            {
                ValidationError err = new ValidationError("This must be only foobar", 100, false, "Value");
                errors.Add(err);
            }
        }

        return errors;
    }
}

And then you bind this validator to you activity class

[ActivityValidator(typeof(CustomActivityValidator))]
class CustomActivity : Activity
{
    // Your activity code here
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM