locked
Activity Data Model - Why do we need it? RRS feed

  • Question

  • I see all the examples for custom activities use InArgument<T> whenever they need to take input. I would really appreciate if someone can explain to me what the problems with the following example would be if I don't use InArgument for my Text property. Thanks.

    using System.Activities;
    using System.Activities.Statements;
    
    namespace ConsoleApplication1
    {
     class Program
     {
      static void Main(string[] args)
      {
       Activity workflow = new Sequence()
       {
        Activities =
        {
         new WriteLine() { Text = "Foo" },
         new WriteLine() { Text = "Bar" }
        }
       };
    
       WorkflowInvoker.Invoke(workflow);
    
      }
    
      public class WriteLine : CodeActivity
      {
       //public InArgument<string> Text { get; set; }
       public string Text { get; set; }
    
       protected override void Execute(CodeActivityContext context)
       {
        //Console.WriteLine(Text.Get(context));
        Console.WriteLine(Text);
       }
      }
     }
    
    
    
    }

     

    Monday, May 3, 2010 5:28 AM

Answers

  • when you declare the property as InArgument<>, you can dynamically input value when you execute the workflow.

    for example, you can take a look at following sample about how to dynamically input value instead of setting static value when you creating the workflow

     

    Sequence seq = new Sequence();
              
                AddToCollection<string> addToCollection = new AddToCollection<string>();
                DynamicActivityProperty property = new DynamicActivityProperty();
                property.Name = "argument1";
                property.Type = typeof(InArgument<ICollection<string>>);
                VisualBasicValue<ICollection<string>> value = new VisualBasicValue<ICollection<string>>();
                value.ExpressionText = "argument1";
                addToCollection.Collection = new InArgument<ICollection<string>>(value);
                Dictionary<string, object> inputs = new Dictionary<string, object>();
                inputs.Add("argument1", new List<string>());
                DynamicActivity activity = new DynamicActivity();
                activity.Properties.Add(property);

    seq.Activities.Add(addToCollection);
                activity.Implementation = () => seq;
                    WorkflowInvoker.Invoke(activity, inputs);
               
                System.Console.ReadLine();

     

    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Monday, May 3, 2010 6:11 AM
  • In a simple case like this no problem.

     

    But when you start updating the Text property in your workflow and the workflow gets persisted to disk you lose all changes because the workflow runtime is not aware of the data.

     

    Maurice 

    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Monday, May 3, 2010 6:26 AM
  • Hi, Josh

    When a instance of WF4 start running, workflow host(workflowApplication) will register all the Arguments,Variables and sub-activities to execution context. When the workflow is persisted in a durable store. the persisted data is the context rather than the object of the workflow.

    So, if don't use InArgument for your Text property, the Text data will be lost when the workflow reloaded from persisted store(You will lost other functions if you don't use InArgument).

    One of the goals of workflow is to develop long running applications, it will be a overkill to use WF to design applications running just in a very short timespan.

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support
    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Tuesday, May 4, 2010 8:12 AM

All replies

  • when you declare the property as InArgument<>, you can dynamically input value when you execute the workflow.

    for example, you can take a look at following sample about how to dynamically input value instead of setting static value when you creating the workflow

     

    Sequence seq = new Sequence();
              
                AddToCollection<string> addToCollection = new AddToCollection<string>();
                DynamicActivityProperty property = new DynamicActivityProperty();
                property.Name = "argument1";
                property.Type = typeof(InArgument<ICollection<string>>);
                VisualBasicValue<ICollection<string>> value = new VisualBasicValue<ICollection<string>>();
                value.ExpressionText = "argument1";
                addToCollection.Collection = new InArgument<ICollection<string>>(value);
                Dictionary<string, object> inputs = new Dictionary<string, object>();
                inputs.Add("argument1", new List<string>());
                DynamicActivity activity = new DynamicActivity();
                activity.Properties.Add(property);

    seq.Activities.Add(addToCollection);
                activity.Implementation = () => seq;
                    WorkflowInvoker.Invoke(activity, inputs);
               
                System.Console.ReadLine();

     

    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Monday, May 3, 2010 6:11 AM
  • In a simple case like this no problem.

     

    But when you start updating the Text property in your workflow and the workflow gets persisted to disk you lose all changes because the workflow runtime is not aware of the data.

     

    Maurice 

    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Monday, May 3, 2010 6:26 AM
  • Hi, Josh

    When a instance of WF4 start running, workflow host(workflowApplication) will register all the Arguments,Variables and sub-activities to execution context. When the workflow is persisted in a durable store. the persisted data is the context rather than the object of the workflow.

    So, if don't use InArgument for your Text property, the Text data will be lost when the workflow reloaded from persisted store(You will lost other functions if you don't use InArgument).

    One of the goals of workflow is to develop long running applications, it will be a overkill to use WF to design applications running just in a very short timespan.

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support
    • Marked as answer by Andrew_Zhu Monday, May 10, 2010 9:04 AM
    Tuesday, May 4, 2010 8:12 AM