Esempio n. 1
0
        private void OnAddApprovalStep(object sender, EventArgs e)
        {
            InvokeWorkflowActivity invokeApprovalStepWorkflow = new InvokeWorkflowActivity();

            //
            // use WorkflowChanges class to author dynamic change
            //
            WorkflowChanges changes = new WorkflowChanges(this);
            //
            // setup to invoke ApprovalStepWorkflow type
            Type type = typeof(ApprovalStepWorkflow);
            invokeApprovalStepWorkflow.Name = "AddApprovalStepWorkflow";
            invokeApprovalStepWorkflow.TargetWorkflow = type;
            //
            // insert invokeApprovalStepWorkflow in ifElseApproval transient activity collection
            //
            CompositeActivity checkApproval = changes.TransientWorkflow.Activities["CheckApproval"] as CompositeActivity;
            CompositeActivity approvedBranch = checkApproval.Activities["Approved"] as CompositeActivity;
            approvedBranch.Activities.Add(invokeApprovalStepWorkflow);
            //
            // apply transient changes to instance
            //
            this.ApplyWorkflowChanges(changes);
            Console.WriteLine("  Added an InvokeWorkflow activity within the workflow to approve the PO");
         }
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            System.Workflow.Activities.Common.WalkerEventHandler handler = null;
            ValidationErrorCollection validationErrors = base.Validate(manager, obj);
            InvokeWorkflowActivity    activity         = obj as InvokeWorkflowActivity;

            if (activity == null)
            {
                throw new ArgumentException(SR.GetString("Error_UnexpectedArgumentType", new object[] { typeof(InvokeWorkflowActivity).FullName }), "obj");
            }
            if (activity.TargetWorkflow == null)
            {
                ValidationError error = new ValidationError(SR.GetString("Error_TypePropertyInvalid", new object[] { "TargetWorkflow" }), 0x116)
                {
                    PropertyName = "TargetWorkflow"
                };
                validationErrors.Add(error);
                return(validationErrors);
            }
            ITypeProvider service = (ITypeProvider)manager.GetService(typeof(ITypeProvider));

            if (service == null)
            {
                throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(ITypeProvider).FullName }));
            }
            Type targetWorkflow = activity.TargetWorkflow;

            if ((targetWorkflow.Assembly == null) && (service.LocalAssembly != null))
            {
                Type type = service.LocalAssembly.GetType(targetWorkflow.FullName);
                if (type != null)
                {
                    targetWorkflow = type;
                }
            }
            if (!TypeProvider.IsAssignable(typeof(Activity), targetWorkflow))
            {
                ValidationError error2 = new ValidationError(SR.GetString("Error_TypeIsNotRootActivity", new object[] { "TargetWorkflow" }), 0x60e)
                {
                    PropertyName = "TargetWorkflow"
                };
                validationErrors.Add(error2);
                return(validationErrors);
            }
            Activity seedActivity = null;

            try
            {
                seedActivity = Activator.CreateInstance(targetWorkflow) as Activity;
            }
            catch (Exception)
            {
            }
            if (seedActivity == null)
            {
                ValidationError error3 = new ValidationError(SR.GetString("Error_GetCalleeWorkflow", new object[] { activity.TargetWorkflow }), 0x500)
                {
                    PropertyName = "TargetWorkflow"
                };
                validationErrors.Add(error3);
                return(validationErrors);
            }
            System.Workflow.Activities.Common.Walker walker = new System.Workflow.Activities.Common.Walker();
            if (handler == null)
            {
                handler = delegate(System.Workflow.Activities.Common.Walker w, System.Workflow.Activities.Common.WalkerEventArgs args) {
                    if ((args.CurrentActivity is WebServiceInputActivity) && ((WebServiceInputActivity)args.CurrentActivity).IsActivating)
                    {
                        ValidationError item = new ValidationError(SR.GetString("Error_ExecWithActivationReceive"), 0x614)
                        {
                            PropertyName = "Name"
                        };
                        validationErrors.Add(item);
                        args.Action = System.Workflow.Activities.Common.WalkerAction.Abort;
                    }
                };
            }
            walker.FoundActivity += handler;
            walker.Walk(seedActivity);
            bool flag = false;

            for (Activity activity3 = activity.Parent; activity3 != null; activity3 = activity3.Parent)
            {
                if ((activity3 is CompensatableTransactionScopeActivity) || (activity3 is TransactionScopeActivity))
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                ValidationError error4 = new ValidationError(SR.GetString("Error_ExecInAtomicScope"), 0x502);
                validationErrors.Add(error4);
            }
            foreach (WorkflowParameterBinding binding in activity.ParameterBindings)
            {
                PropertyInfo property = null;
                property = targetWorkflow.GetProperty(binding.ParameterName);
                if (property == null)
                {
                    ValidationError error5 = new ValidationError(SR.GetString("Error_ParameterNotFound", new object[] { binding.ParameterName }), 0x504);
                    if (InvokeWorkflowActivity.ReservedParameterNames.Contains(binding.ParameterName))
                    {
                        error5.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity.GetType(), binding.ParameterName);
                    }
                    validationErrors.Add(error5);
                }
                else
                {
                    Type propertyType = property.PropertyType;
                    if (binding.GetBinding(WorkflowParameterBinding.ValueProperty) != null)
                    {
                        ValidationErrorCollection errors = System.Workflow.Activities.Common.ValidationHelpers.ValidateProperty(manager, activity, binding.GetBinding(WorkflowParameterBinding.ValueProperty), new PropertyValidationContext(binding, null, binding.ParameterName), new BindValidationContext(propertyType, AccessTypes.Read));
                        if (errors.Count != 0)
                        {
                            validationErrors.AddRange(errors);
                        }
                    }
                }
            }
            return(validationErrors);
        }
Esempio n. 3
0
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection validationErrors = base.Validate(manager, obj);

            InvokeWorkflowActivity invokeWorkflow = obj as InvokeWorkflowActivity;

            if (invokeWorkflow == null)
            {
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(InvokeWorkflowActivity).FullName), "obj");
            }

            if (invokeWorkflow.TargetWorkflow == null)
            {
                ValidationError error = new ValidationError(SR.GetString(SR.Error_TypePropertyInvalid, "TargetWorkflow"), ErrorNumbers.Error_PropertyNotSet);
                error.PropertyName = "TargetWorkflow";
                validationErrors.Add(error);
            }
            else
            {
                ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider));
                if (typeProvider == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(ITypeProvider).FullName));
                }

                Type targetWorkflowType = invokeWorkflow.TargetWorkflow;
                if (targetWorkflowType.Assembly == null && typeProvider.LocalAssembly != null)
                {
                    Type workflowType = typeProvider.LocalAssembly.GetType(targetWorkflowType.FullName);
                    if (workflowType != null)
                    {
                        targetWorkflowType = workflowType;
                    }
                }

                if (!TypeProvider.IsAssignable(typeof(Activity), targetWorkflowType))
                {
                    ValidationError error = new ValidationError(SR.GetString(SR.Error_TypeIsNotRootActivity, "TargetWorkflow"), ErrorNumbers.Error_TypeIsNotRootActivity);
                    error.PropertyName = "TargetWorkflow";
                    validationErrors.Add(error);
                }
                else
                {
                    Activity rootActivity = null;
                    try
                    {
                        rootActivity = Activator.CreateInstance(targetWorkflowType) as Activity;
                    }
                    catch (Exception)
                    {
                        //
                    }

                    if (rootActivity == null)
                    {
                        ValidationError error = new ValidationError(SR.GetString(SR.Error_GetCalleeWorkflow, invokeWorkflow.TargetWorkflow), ErrorNumbers.Error_GetCalleeWorkflow);
                        error.PropertyName = "TargetWorkflow";
                        validationErrors.Add(error);
                    }
                    else
                    {
                        // Exec can't have activate receive.
                        Walker walker = new Walker();
                        walker.FoundActivity += delegate(Walker w, WalkerEventArgs args)
                        {
                            if ((args.CurrentActivity is WebServiceInputActivity && ((WebServiceInputActivity)args.CurrentActivity).IsActivating))
                            {
                                ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ExecWithActivationReceive), ErrorNumbers.Error_ExecWithActivationReceive);
                                validationError.PropertyName = "Name";
                                validationErrors.Add(validationError);

                                args.Action = WalkerAction.Abort;
                            }
                        };

                        walker.Walk((Activity)rootActivity);

                        bool     inAtomicScope = false;
                        Activity parentScope   = invokeWorkflow.Parent;
                        while (parentScope != null)
                        {
                            if (parentScope is CompensatableTransactionScopeActivity || parentScope is TransactionScopeActivity)
                            {
                                inAtomicScope = true;
                                break;
                            }
                            parentScope = parentScope.Parent;
                        }

                        // Validate that if the workflow is transactional or being exec'd then it is not enclosed in an atomic scope.
                        if (inAtomicScope)
                        {
                            ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ExecInAtomicScope), ErrorNumbers.Error_ExecInAtomicScope);
                            validationErrors.Add(validationError);
                        }

                        foreach (WorkflowParameterBinding paramBinding in invokeWorkflow.ParameterBindings)
                        {
                            PropertyInfo propertyInfo = null;

                            propertyInfo = targetWorkflowType.GetProperty(paramBinding.ParameterName);
                            if (propertyInfo == null)
                            {
                                ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ParameterNotFound, paramBinding.ParameterName), ErrorNumbers.Error_ParameterNotFound);
                                if (InvokeWorkflowActivity.ReservedParameterNames.Contains(paramBinding.ParameterName))
                                {
                                    validationError.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(invokeWorkflow.GetType(), paramBinding.ParameterName);
                                }

                                validationErrors.Add(validationError);
                                continue;
                            }

                            Type parameterType = propertyInfo.PropertyType;
                            if (paramBinding.GetBinding(WorkflowParameterBinding.ValueProperty) != null)
                            {
                                ValidationErrorCollection memberErrors = ValidationHelpers.ValidateProperty(manager, invokeWorkflow, paramBinding.GetBinding(WorkflowParameterBinding.ValueProperty), new PropertyValidationContext(paramBinding, null, paramBinding.ParameterName), new BindValidationContext(parameterType, AccessTypes.Read));

                                if (memberErrors.Count != 0)
                                {
                                    validationErrors.AddRange(memberErrors);
                                    continue;
                                }
                            }
                        }
                    }
                }
            }

            return(validationErrors);
        }