WF4: Passing Arguments to Activities

In Windows Workflow Foundation .NET 4 (WF4) everything is an Activity.  That is a class that inherits from System.Activities.Activity.  Activities can have in, out or in/out arguments and do their work then the Activity.Execute() method is called.  You could think of them as being similar to a static method except for the fact that a static method can have only 1 return argument where an Activity can have more than one. 

Passing arguments to an activity can be done one of two ways.  You can use Initialization syntax when creating the activity to set the input arguments (which are just public properties of type InArgument<T>) or you can pass a Dictionary<string, object> which will be used to initialize the arguments.

Passing Arguments with Initialization syntax

    1: [TestMethod]
    2: public void ShouldReturnGreetingWithName()
    3: {
    4:     var output = WorkflowInvoker.Invoke(
    5:         new SayHello()
    6:         {
    7:             UserName = "Test"
    8:         });
    9:     Assert.AreEqual("Hello Test from Workflow 4", output["Greeting"]);
   10: }

This works well except that out arguments (OutArgument<T>) will also show up in the Intellisense window as well as other public properties such as the DisplayName.  In this example from the beta 2 release of the “Introduction to Workflow 4” hands on lab (look for it at the PDC) you will see three properties from Intellisense but only one of them is an in argument suitable for initialization.

OutArg

As you can see initializing an OutArgument looks possible, but if you try it…

image

What’s going on here?  InArgument<T> will implicitly construct from an argument of type T but OutArgument<T> won’t do this so even though it appears like you can do this, you can’t because it won’t compile.  But this is a good thing because you shouldn’t initialize an OutArgument anyway.

In practice, I think most people will wrap activity initialization and (possibly) invocation inside of other methods so that developers who are unfamiliar with workflow can just call those methods to do the work.