In a WF4 rehosting Application I need to validate an Activity based on custom parameters, which are stored in a database.
When the user entered some values and the selection is changed in the designer the overwritten CacheMetadata
Method is called as follows:
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
if (!ParamCache.ContainsKey(InstanceId.ToString())) return;
var paramsToValidate = ParamCache.Get(InstanceId.ToString());
var validationErrors = (from paramToValidate in paramsToValidate
let errorMessage = paramToValidate.Validate()
where !string.IsNullOrEmpty(errorMessage)
select new ValidationError(errorMessage, false)).ToList();
foreach (var validationError in validationErrors)
{
metadata.AddValidationError(validationError);
}
}
During runtime the error collection is evaluated using this Validate Method:
public void Validate()
{
var activity = ModelItem.GetCurrentValue() as Activity;
if (activity != null)
{
var validationResults = ActivityValidationServices.Validate(activity);
var designerRegionVieWModel = Parent as DesignerRegionViewModel;
IsValid = validationResults.Errors.Count <= 0;
...
}
Although ValidationError
objects have been added, and IsValid
is false the designer is not updated to visualize the errors with the red exclamation mark style.
However, if I add some Errors (for testing purpose) right on the beginning of the CacheMetadata
Method the errors are displayed as required.
Could this be some timing problem or is there some additional runtime evaluation which has to be triggered?
I found out how it works. The entire Workflow needs to be Re-Evaluated. You can achieve this by using the WorkflowDesigner's ValidationService
.
var validationService = workflowDesigner.Context.Services.GetService<ValidationService>();
validationService.ValidateWorkflow();