locked
How we can validate workflow XAML file RRS feed

  • Question

  • Hello Everyone,

    I am creating xaml file in workflow designer and saving this file into database as string format.After retrieving this string from database , i want to validate that XAML contain any workflow error or not.

    Tuesday, July 12, 2011 8:37 AM

Answers

  • Thanks George , it helped me to resolve my problem .

    pramodsinghnegi

    Below function take string format of xaml and return list of errors .

     

     

      private List<string>  ValidateWorlflow(string xamlFile)

            {

                List<string> listError = new List<string>();

                DynamicActivity activity = null;

     

                try

                {

                    // load the xaml 

                    activity = System.Activities.XamlIntegration.ActivityXamlServices.Load(new StringReader(xamlFile)) as DynamicActivity;

                }

                catch (Exception ex)

                {

                    listError.Add("In the workflow contains following errors and warnings :");

                    listError.Add(ex.Message);

                    return listError;

                    // logger.Error("Unable to load workflow", ex);

                   // throw new InvalidWorkflowException("Workflow is not valid", ex);

                }

                // validate the activity through ActivityValidationServices

                System.Activities.Validation.ValidationResults results = System.Activities.Validation.ActivityValidationServices.Validate(activity);

                // checking whether workflow has any errors or not 

                if (results.Errors.Count > 0)

                {

                    listError.Add("In the workflow contains following errors and warnings :");

                    foreach (System.Activities.Validation.ValidationError error in results.Errors)

                    {

                        listError.Add(error.Message);

     

                    }

                    // adding worklfow warnings in the listError 

                    foreach (System.Activities.Validation.ValidationError warning in results.Warnings)

                    {

                        listError.Add(warning.Message);

                    }

                }

     

                return listError;

     

            }

     

    Wednesday, July 13, 2011 10:03 AM

All replies

  • The block of code we use to load workflows from the database is:

     

     private static Activity LoadAndValidateProgram(Stream stream, bool validate = true)
        {
          Activity activity = null;
    
          try
          {
            activity = ActivityXamlServices.Load(stream);
          }
          catch (Exception ex)
          {
            logger.Error("Unable to load workflow", ex);
            throw new InvalidWorkflowException("Workflow is not valid", ex);
          }
          if (validate)
          {
            ValidationResults results = ActivityValidationServices.Validate(activity);
    
            foreach (ValidationError error in results.Errors)
            {
              logger.ErrorFormat("{0} Activity: {1}",
                error.Message,
                error.Source.DisplayName);
            }
    
            foreach (ValidationError warning in results.Warnings)
            {
              logger.WarnFormat("{0} Activity: {1}",
                warning.Message,
                warning.Source.DisplayName);
            }
    
            if (results.Errors.Count > 0)
            {
              string msg = string.Format("This workflow has validation errors");
              throw new InvalidWorkflowException(msg);
            }
          }
          return activity;
        }
    


    Hope it helps.

    George

    Tuesday, July 12, 2011 10:02 AM
  • Thanks George , it helped me to resolve my problem .

    pramodsinghnegi

    Below function take string format of xaml and return list of errors .

     

     

      private List<string>  ValidateWorlflow(string xamlFile)

            {

                List<string> listError = new List<string>();

                DynamicActivity activity = null;

     

                try

                {

                    // load the xaml 

                    activity = System.Activities.XamlIntegration.ActivityXamlServices.Load(new StringReader(xamlFile)) as DynamicActivity;

                }

                catch (Exception ex)

                {

                    listError.Add("In the workflow contains following errors and warnings :");

                    listError.Add(ex.Message);

                    return listError;

                    // logger.Error("Unable to load workflow", ex);

                   // throw new InvalidWorkflowException("Workflow is not valid", ex);

                }

                // validate the activity through ActivityValidationServices

                System.Activities.Validation.ValidationResults results = System.Activities.Validation.ActivityValidationServices.Validate(activity);

                // checking whether workflow has any errors or not 

                if (results.Errors.Count > 0)

                {

                    listError.Add("In the workflow contains following errors and warnings :");

                    foreach (System.Activities.Validation.ValidationError error in results.Errors)

                    {

                        listError.Add(error.Message);

     

                    }

                    // adding worklfow warnings in the listError 

                    foreach (System.Activities.Validation.ValidationError warning in results.Warnings)

                    {

                        listError.Add(warning.Message);

                    }

                }

     

                return listError;

     

            }

     

    Wednesday, July 13, 2011 10:03 AM