public void Declare(LocationReference locationReference, Activity owner, ref IList<ValidationError> validationErrors) { if (locationReference.Name == null) { if (this.unnamedDeclarations == null) { this.unnamedDeclarations = new List<LocationReference>(); } this.unnamedDeclarations.Add(locationReference); } else if (this.Declarations.ContainsKey(locationReference.Name)) { string id = null; if (owner != null) { id = owner.Id; } ValidationError data = new ValidationError(System.Activities.SR.SymbolNamesMustBeUnique(locationReference.Name)) { Source = owner, Id = id }; ActivityUtilities.Add<ValidationError>(ref validationErrors, data); } else { this.Declarations.Add(locationReference.Name, locationReference); } }
internal bool InitializeRelationship(Activity parent, bool isPublic, ref IList<ValidationError> validationErrors) { if ((this.cacheId == parent.CacheId) && (this.Owner != null)) { ValidationError data = new ValidationError(System.Activities.SR.VariableAlreadyInUseOnActivity(this.Name, parent.DisplayName, this.Owner.DisplayName), false, this.Name, parent); ActivityUtilities.Add<ValidationError>(ref validationErrors, data); return false; } this.Owner = parent; this.cacheId = parent.CacheId; this.IsPublic = isPublic; if (this.Default == null) { return true; } ActivityWithResult innerExpression = this.Default; if (innerExpression is Argument.IExpressionWrapper) { innerExpression = ((Argument.IExpressionWrapper) innerExpression).InnerExpression; } if (innerExpression.ResultType != base.Type) { ActivityUtilities.Add<ValidationError>(ref validationErrors, new ValidationError(System.Activities.SR.VariableExpressionTypeMismatch(this.Name, base.Type, innerExpression.ResultType), false, this.Name, parent)); } return this.Default.InitializeRelationship(this, isPublic, ref validationErrors); }
public ValidationErrorInfo(ValidationError validationError) { this.Id = validationError.Id; this.Message = validationError.Message; this.PropertyName = validationError.PropertyName; this.FileName = string.Empty; this.IsWarning = validationError.IsWarning; }
public static void AddValidationError(NativeActivityContext context, ValidationError error) { List<ValidationError> list = context.Properties.Find("System.Activities.Validation.Constraint.ValidationErrorList") as List<ValidationError>; if (list == null) { throw FxTrace.Exception.AsError(new InvalidOperationException(System.Activities.SR.AddValidationErrorMustBeCalledFromConstraint(typeof(Constraint).Name))); } list.Add(error); }
internal bool InitializeRelationship(Activity parent, ref IList<ValidationError> validationErrors) { if (this.cacheId == parent.CacheId) { ValidationError data = new ValidationError(System.Activities.SR.DelegateArgumentAlreadyInUseOnActivity(this.Name, parent.DisplayName, this.Owner.DisplayName), this.Owner); ActivityUtilities.Add<ValidationError>(ref validationErrors, data); return false; } this.Owner = parent; this.cacheId = parent.CacheId; return true; }
private void ValidateExpressionSubtree(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain) { if (childActivity.Activity.InternalCanInduceIdle) { Activity activity = childActivity.Activity; Activity expressionRoot = this.expressionRoot; RuntimeArgument boundRuntimeArgument = ActivityValidationServices.GetBoundRuntimeArgument(expressionRoot); ValidationError data = new ValidationError(System.Activities.SR.CanInduceIdleActivityInArgumentExpression(boundRuntimeArgument.Name, expressionRoot.Parent.DisplayName, activity.DisplayName), true, boundRuntimeArgument.Name, expressionRoot.Parent); ActivityUtilities.Add<ValidationError>(ref this.errors, data); } }
private void ValidateElement(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain) { Activity objA = childActivity.Activity; if (!this.settings.SingleLevel || object.ReferenceEquals(objA, this.rootToValidate)) { if (this.settings.HasAdditionalConstraints) { bool suppressGetChildrenViolations = this.settings.OnlyUseAdditionalConstraints || this.settings.SingleLevel; for (Type type = objA.GetType(); type != null; type = type.BaseType) { IList<Constraint> list; if (this.settings.AdditionalConstraints.TryGetValue(type, out list)) { ActivityValidationServices.RunConstraints(childActivity, parentChain, list, this.options, suppressGetChildrenViolations, ref this.errors); } if (type.IsGenericType) { IList<Constraint> list2; Type genericTypeDefinition = type.GetGenericTypeDefinition(); if ((genericTypeDefinition != null) && this.settings.AdditionalConstraints.TryGetValue(genericTypeDefinition, out list2)) { ActivityValidationServices.RunConstraints(childActivity, parentChain, list2, this.options, suppressGetChildrenViolations, ref this.errors); } } } } if (childActivity.Activity.IsExpressionRoot) { if (childActivity.Activity.HasNonEmptySubtree) { this.expressionRoot = childActivity.Activity; ActivityUtilities.FinishCachingSubtree(childActivity, parentChain, ProcessActivityTreeOptions.FullCachingOptions, new ActivityUtilities.ProcessActivityCallback(this.ValidateExpressionSubtree)); this.expressionRoot = null; } else if (childActivity.Activity.InternalCanInduceIdle) { Activity activity = childActivity.Activity; RuntimeArgument boundRuntimeArgument = ActivityValidationServices.GetBoundRuntimeArgument(activity); ValidationError data = new ValidationError(System.Activities.SR.CanInduceIdleActivityInArgumentExpression(boundRuntimeArgument.Name, activity.Parent.DisplayName, activity.DisplayName), true, boundRuntimeArgument.Name, activity.Parent); ActivityUtilities.Add<ValidationError>(ref this.errors, data); } } } }
internal static void RunConstraints(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain, IList<Constraint> constraints, ProcessActivityTreeOptions options, bool suppressGetChildrenViolations, ref IList<ValidationError> validationErrors) { if (constraints != null) { Activity activity = childActivity.Activity; LocationReferenceEnvironment parentEnvironment = activity.GetParentEnvironment(); Dictionary<string, object> inputs = new Dictionary<string, object>(2); for (int i = 0; i < constraints.Count; i++) { Constraint workflow = constraints[i]; if (workflow != null) { object obj2; inputs["ToValidate"] = activity; ValidationContext context = new ValidationContext(childActivity, parentChain, options, parentEnvironment); inputs["ToValidateContext"] = context; IDictionary<string, object> dictionary2 = null; try { dictionary2 = WorkflowInvoker.Invoke(workflow, inputs); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } ValidationError data = new ValidationError(System.Activities.SR.InternalConstraintException(workflow.DisplayName, activity.GetType().FullName, activity.DisplayName, exception.ToString()), false) { Source = activity, Id = activity.Id }; ActivityUtilities.Add<ValidationError>(ref validationErrors, data); } if ((dictionary2 != null) && dictionary2.TryGetValue("ViolationList", out obj2)) { IList<ValidationError> list = (IList<ValidationError>) obj2; if (list.Count > 0) { Activity activity2; if (validationErrors == null) { validationErrors = new List<ValidationError>(); } string str = GenerateValidationErrorPrefix(childActivity.Activity, parentChain, out activity2); for (int j = 0; j < list.Count; j++) { ValidationError item = list[j]; item.Source = activity2; item.Id = activity2.Id; if (!string.IsNullOrEmpty(str)) { item.Message = str + item.Message; } validationErrors.Add(item); } } } if (!suppressGetChildrenViolations) { context.AddGetChildrenErrors(ref validationErrors); } } } } }
internal static void RunConstraints(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain, IList <Constraint> constraints, ProcessActivityTreeOptions options, bool suppressGetChildrenViolations, ref IList <ValidationError> validationErrors) { if (constraints != null) { Activity activity = childActivity.Activity; LocationReferenceEnvironment parentEnvironment = activity.GetParentEnvironment(); Dictionary <string, object> inputs = new Dictionary <string, object>(2); for (int i = 0; i < constraints.Count; i++) { Constraint workflow = constraints[i]; if (workflow != null) { object obj2; inputs["ToValidate"] = activity; ValidationContext context = new ValidationContext(childActivity, parentChain, options, parentEnvironment); inputs["ToValidateContext"] = context; IDictionary <string, object> dictionary2 = null; try { dictionary2 = WorkflowInvoker.Invoke(workflow, inputs); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } ValidationError data = new ValidationError(System.Activities.SR.InternalConstraintException(workflow.DisplayName, activity.GetType().FullName, activity.DisplayName, exception.ToString()), false) { Source = activity, Id = activity.Id }; ActivityUtilities.Add <ValidationError>(ref validationErrors, data); } if ((dictionary2 != null) && dictionary2.TryGetValue("ViolationList", out obj2)) { IList <ValidationError> list = (IList <ValidationError>)obj2; if (list.Count > 0) { Activity activity2; if (validationErrors == null) { validationErrors = new List <ValidationError>(); } string str = GenerateValidationErrorPrefix(childActivity.Activity, parentChain, out activity2); for (int j = 0; j < list.Count; j++) { ValidationError item = list[j]; item.Source = activity2; item.Id = activity2.Id; if (!string.IsNullOrEmpty(str)) { item.Message = str + item.Message; } validationErrors.Add(item); } } } if (!suppressGetChildrenViolations) { context.AddGetChildrenErrors(ref validationErrors); } } } } }
// Mark a single "child has an error" on a parent private void MarkParent(ValidationError validationError, object errorParent) { ValidationState childValidationState = GetValidationState(validationError); ValidationErrorState currentError; if (!this.ValidationErrors.TryGetValue(errorParent, out currentError)) { currentError = ChildInvalidError(); this.ValidationErrors.Add(errorParent, currentError); } if (currentError.ValidationState < childValidationState) { currentError.ValidationState = childValidationState; } this.NotifyValidationPropertiesChanged(errorParent); }
// Mark a single error on the culprit private void MarkCulprit(object errorSource, ValidationError validationError) { ValidationErrorState currentError; if (!this.ValidationErrors.TryGetValue(errorSource, out currentError)) { currentError = new ValidationErrorState(new List<string>(), ValidationState.Valid); this.ValidationErrors.Add(errorSource, currentError); } MergeValidationError(currentError, validationError); this.NotifyValidationPropertiesChanged(errorSource); }
// Mark a single error including its parent chain private void MarkError(ValidationError validationError, List<object> errorSourcePath) { object errorSource = errorSourcePath[0]; this.MarkCulprit(errorSource, validationError); // Intentionally skipping the zeroth errorSourcePath because that is the culprit and is marked above. for (int errorSourcePathIndex = 1; errorSourcePathIndex < errorSourcePath.Count; errorSourcePathIndex++) { this.MarkParent(validationError, errorSourcePath[errorSourcePathIndex]); } foreach (object parent in this.GetParentChainWithSource(validationError.Source)) { this.MarkParent(validationError, parent); } }
private void ValidateElement(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain) { var toValidate = childActivity.Activity; if (!this.settings.SingleLevel || object.ReferenceEquals(toValidate, this.rootToValidate)) { // 0. Open time violations are captured by the CacheMetadata walk. // 1. Argument validations are done by the CacheMetadata walk. // 2. Build constraints are done by the CacheMetadata walk. // 3. Then do policy constraints if (this.settings.HasAdditionalConstraints && childActivity.CanBeExecuted && parentChain.WillExecute) { var suppressGetChildrenViolations = this.settings.OnlyUseAdditionalConstraints || this.settings.SingleLevel; var currentType = toValidate.GetType(); while (currentType != null) { if (this.settings.AdditionalConstraints.TryGetValue(currentType, out var policyConstraints)) { RunConstraints(childActivity, parentChain, policyConstraints, this.options, suppressGetChildrenViolations, ref this.errors); } if (currentType.IsGenericType) { var genericDefinitionType = currentType.GetGenericTypeDefinition(); if (genericDefinitionType != null) { if (this.settings.AdditionalConstraints.TryGetValue(genericDefinitionType, out var genericTypePolicyConstraints)) { RunConstraints(childActivity, parentChain, genericTypePolicyConstraints, this.options, suppressGetChildrenViolations, ref this.errors); } } } currentType = currentType.BaseType; } } //4. Validate if the argument expression subtree contains an activity that can induce idle. if (childActivity.Activity.IsExpressionRoot) { if (childActivity.Activity.HasNonEmptySubtree) { this.expressionRoot = childActivity.Activity; // Back-compat: In Dev10 we always used ProcessActivityTreeOptions.FullCachingOptions here, and ignored this.options. // So we need to continue to do that, unless the new Dev11 flag SkipRootConfigurationValidation is passed. var options = this.options.SkipRootConfigurationValidation ? this.options : ProcessActivityTreeOptions.FullCachingOptions; ActivityUtilities.FinishCachingSubtree(childActivity, parentChain, options, ValidateExpressionSubtree); this.expressionRoot = null; } else if (childActivity.Activity.InternalCanInduceIdle) { var activity = childActivity.Activity; var runtimeArgument = GetBoundRuntimeArgument(activity); var error = new ValidationError(SR.CanInduceIdleActivityInArgumentExpression(runtimeArgument.Name, activity.Parent.DisplayName, activity.DisplayName), true, runtimeArgument.Name, activity.Parent); ActivityUtilities.Add(ref this.errors, error); } } } }
internal static void RunConstraints(ActivityUtilities.ChildActivity childActivity, ActivityUtilities.ActivityCallStack parentChain, IList <Constraint> constraints, ProcessActivityTreeOptions options, bool suppressGetChildrenViolations, ref IList <ValidationError> validationErrors) { if (constraints != null) { var toValidate = childActivity.Activity; var environment = toValidate.GetParentEnvironment(); var inputDictionary = new Dictionary <string, object>(2); for (var constraintIndex = 0; constraintIndex < constraints.Count; constraintIndex++) { var constraint = constraints[constraintIndex]; // there may be null entries here if (constraint == null) { continue; } inputDictionary[Constraint.ToValidateArgumentName] = toValidate; var validationContext = new ValidationContext(childActivity, parentChain, options, environment); inputDictionary[Constraint.ToValidateContextArgumentName] = validationContext; IDictionary <string, object> results = null; try { results = WorkflowInvoker.Invoke(constraint, inputDictionary); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } var constraintExceptionValidationError = new ValidationError(SR.InternalConstraintException(constraint.DisplayName, toValidate.GetType().FullName, toValidate.DisplayName, e.ToString()), false) { Source = toValidate, Id = toValidate.Id }; ActivityUtilities.Add(ref validationErrors, constraintExceptionValidationError); } if (results != null) { if (results.TryGetValue(Constraint.ValidationErrorListArgumentName, out var resultValidationErrors)) { var validationErrorList = (IList <ValidationError>)resultValidationErrors; if (validationErrorList.Count > 0) { if (validationErrors == null) { validationErrors = new List <ValidationError>(); } var prefix = ActivityValidationServices.GenerateValidationErrorPrefix(childActivity.Activity, parentChain, options, out var source); for (var validationErrorIndex = 0; validationErrorIndex < validationErrorList.Count; validationErrorIndex++) { var validationError = validationErrorList[validationErrorIndex]; validationError.Source = source; validationError.Id = source.Id; if (!string.IsNullOrEmpty(prefix)) { validationError.Message = prefix + validationError.Message; } validationErrors.Add(validationError); } } } } if (!suppressGetChildrenViolations) { validationContext.AddGetChildrenErrors(ref validationErrors); } } } }
internal bool InitializeRelationship(Activity parent, ref IList<ValidationError> validationErrors) { if (this.cacheId == parent.CacheId) { Fx.Assert(this.Owner != null, "must have an owner here"); ValidationError validationError = new ValidationError(SR.DelegateArgumentAlreadyInUseOnActivity(this.Name, parent.DisplayName, this.Owner.DisplayName), this.Owner); ActivityUtilities.Add(ref validationErrors, validationError); // Get out early since we've already initialized this argument. return false; } this.Owner = parent; this.cacheId = parent.CacheId; return true; }
private static ValidationState GetValidationState(ValidationError validationError) { return validationError.IsWarning ? ValidationState.Warning : ValidationState.Error; }
private static void MergeValidationError(ValidationErrorState originalError, ValidationError newError) { if (originalError.ValidationState == ValidationState.ChildInvalid) { // If original error is due to child's issue, clear the error list, // as we don't care about its child's issues anymore and want to add its own issues. originalError.ErrorMessages.Clear(); } ValidationState errorState = GetValidationState(newError); if (originalError.ValidationState < errorState) { // Promote to the higher level of violation. originalError.ValidationState = errorState; } if (newError.IsWarning) { originalError.ErrorMessages.Add(string.Format(CultureInfo.CurrentUICulture, SR.WarningFormat, newError.Message)); } else { originalError.ErrorMessages.Add(newError.Message); } }
public void Declare(LocationReference locationReference, Activity owner, ref IList<ValidationError> validationErrors) { Fx.Assert(locationReference != null, "Must not be null"); if (locationReference.Name == null) { if (this.unnamedDeclarations == null) { this.unnamedDeclarations = new List<LocationReference>(); } this.unnamedDeclarations.Add(locationReference); } else { if (this.Declarations.ContainsKey(locationReference.Name)) { string id = null; if (owner != null) { id = owner.Id; } ValidationError validationError = new ValidationError(SR.SymbolNamesMustBeUnique(locationReference.Name)) { Source = owner, Id = id }; ActivityUtilities.Add(ref validationErrors, validationError); } else { this.Declarations.Add(locationReference.Name, locationReference); } } }
private static string GenerateExceptionString(IList <ValidationError> validationErrors, ExceptionReason reason) { // 4096 is an arbitrary constant. Currently clipped by character count (not bytes). const int maxExceptionStringSize = 4096; StringBuilder exceptionMessageBuilder = null; for (int i = 0; i < validationErrors.Count; i++) { ValidationError validationError = validationErrors[i]; if (!validationError.IsWarning) { // create the common exception string if (exceptionMessageBuilder == null) { exceptionMessageBuilder = new StringBuilder(); switch (reason) { case ExceptionReason.InvalidTree: exceptionMessageBuilder.Append(SR.ErrorsEncounteredWhileProcessingTree); break; case ExceptionReason.InvalidNonNullInputs: exceptionMessageBuilder.Append(SR.RootArgumentViolationsFound); break; case ExceptionReason.InvalidNullInputs: exceptionMessageBuilder.Append(SR.RootArgumentViolationsFoundNoInputs); break; } } string activityName = null; if (validationError.Source != null) { activityName = validationError.Source.DisplayName; } else { activityName = "<UnknownActivity>"; } exceptionMessageBuilder.AppendLine(); exceptionMessageBuilder.Append(string.Format("'{0}': {1}", activityName, validationError.Message)); if (exceptionMessageBuilder.Length > maxExceptionStringSize) { break; } } } string exceptionString = null; if (exceptionMessageBuilder != null) { exceptionString = exceptionMessageBuilder.ToString(); if (exceptionString.Length > maxExceptionStringSize) { string snipNotification = SR.TooManyViolationsForExceptionMessage; exceptionString = exceptionString.Substring(0, maxExceptionStringSize - snipNotification.Length); exceptionString += snipNotification; } } return(exceptionString); }